我正在使用以下方法制作 facet_grid 图:
sample <- read.csv('sample.csv')
ggplot(sample, aes(category, count)) + geom_bar() + facet_grid(. ~ parent_id)
我的数据结构如下:
id,parent_id, category, count
1, 21, C1, 4
2, 21, C2, 7
3, 21, C3, 4
4, 22, D1, 28
5, 22, D2, 20
6, 22, D3, 0
7, 22, D5, 1
8, 22, D6, 4
9, 22, D7, 1
10, 23, E1, 17
11, 23, E2, 33
12, 23, E3, 31
当我对此进行构图时,它看起来像这样:
我想要的是限制每个方面的类别数量,以便我只C1, C2, C3
在第一个图(等)上显示。有没有办法可以限制此处显示的类别数量?
这是dput(sample)
我的 plot 命令的输出,以便可以轻松地复制我的图像:
sample <-
structure(list(id = 1:12, parent_id = c(21L, 21L, 21L, 22L, 22L,
22L, 22L, 22L, 22L, 23L, 23L, 23L), category = structure(1:12, .Label = c(" C1",
" C2", " C3", " D1", " D2", " D3", " D5",
" D6", " D7", " E1", " E2", " E3"), class = "factor"),
count = c(4L, 7L, 4L, 28L, 20L, 0L, 1L, 4L, 1L, 17L, 33L,
31L)), .Names = c("id", "parent_id", "category", "count"), class = "data.frame", row.names = c(NA,
-12L))
ggplot(sample, aes(category, count)) + geom_bar() + facet_grid(. ~ parent_id)