I am making a tilemap in ggplot as below. 2 questions:
1) How can I expand the x-axis limits to label my groups at x = 4? 2) How can I put horizontal lines between Groups (i.e. a line between 1 and 2, 2 and 3, etc.) automatically, not specifying y-value manually?
require(tidyverse)
set.seed(1)
df <- data.frame(ID = as.character(c(1:50)),
Group = sample(1:8, 50, replace = T),
var1 = sample(c('Y', 'N'), 50, replace = T),
var2 = sample(c('Y', 'N'), 50, replace = T),
var3 = sample(c('Y', 'N'), 50, replace = T)) %>%
gather('var', 'y_n', var1:var3) %>%
arrange(-Group) %>%
mutate(ID = factor(ID, levels = unique(ID, ordered = T)))
ggplot(df, aes(var, ID, label = Group))+
geom_tile(aes(fill = y_n), color = 'white')+
scale_fill_manual(values = c('white', 'lightblue'))+
scale_x_discrete(expand = c(0, 0))+
geom_text(x = 3.5, hjust = 'right')
strip.text.y = element_text(angle = 0)
to theme. 2) Check this post to see how to draw only horizontal lines between facets: #28652784. Also: there's no need foraes(..., label = Group)
here. – Ontina