简体   繁体   中英

How to have line breaks in facet_grid?

I have a huge data.table and I want to plot histograms for each group. In the example below I want to have say 6 plots per "line" (so the last line will only have two plots, for the s and t Groups).

How do I do this?

library(ggplot2)
library(data.table)
DT <- data.table(Group = rep(letters[1:20], each = 200),
                 Value = rnorm(4000))

hist.plot <- ggplot(DT, aes(x = Value)) +
                    geom_histogram(binwidth = 0.3, colour = 'black')

hist.plot + facet_grid(. ~ Group)

This produces a very long "line" of plots, but I would like less plots per line: 在此处输入图片说明

Comment to answer:

Replace facet_grid with facet_wrap(~ Group, ncol = 6) . The main strength of facet_grid is when you are mapping different groups to the rows and columns of your facet grid. If you only facet by a single variable, facet_wrap is the preferred choice as you can have line breaks. You can specify your desired number of rows and columns. See ?facet_wrap for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM