My data (multiple sequence alignment) looks like this:
rand.mat <- matrix(sample(c("A", "T", "C", "G"), size = 250 * 50, replace = T),
ncol = 250, nrow = 50)
rownames(rand.mat) <- paste0("gene_", 1:nrow(rand.mat))
rand.df <- rand.mat %>% reshape2::melt()
dir.create("~/test/wrap_plot/", showWarnings = F, recursive = T)
p <- rand.df %>% ggplot(aes(x = Var2, y = Var1, label = value, color = value))
geom_text(size = 2)
theme_bw()
scale_x_continuous(expand = expansion(add = 1))
ggsave("~/test/wrap_plot/before_wrap.png", width = 25, height = 8)
It's much wider than it's tall. This aspect ratio needs to be maintained because readers need to be able to see each letter (I can't squeeze the plot to make it narrower)
However, to present this on a letter paper, I would like to display the second half of the plot on the next row ("wrap" the plot):
I wonder if there is a way to automatically wrap the plot in ggplot2? Thanks!!
CodePudding user response:
rand.df %>%
mutate(half = Var2 %/% median(Var2)) %>%
ggplot(aes(x = Var2, y = Var1, label = value, color = value))
geom_text(size = 2)
scale_x_continuous(expand = expansion(add = 1))
facet_wrap(~half, scales = "free_x", ncol = 1)
theme_bw()
theme(strip.text = element_blank())