限制构面图中构面的类别数量 [英] Limiting the number of categories on facets in a facet plot
问题描述
sample< - read.csv('sample。 csv')
ggplot(sample,aes(category,count))+ geom_bar()+ facet_grid(。〜parent_id)
我的数据结构如下:
id,parent_id,category ,计数
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
(etc )。有没有一种方法可以限制这里显示的类别数量?
这里是 dput(sample)
和我的绘图命令,以便我的图像可以很容易地复制:
sample < -
structure(list(id = 1:12,parent_id = c(21L,21L,21L,22L,22L,
22L,22L,22L,22L ,23L,23L,23L),类别=结构(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。框架,row.names = c(NA,
-12L))
ggplot(sample,aes(category,count))+ geom_bar()+ facet_grid(。〜parent_id)
您可以添加 scales = free_x
到 facet_grid()
。在这种情况下,对于每个方面,只有在用于特定方面的值的范围内才会有x值。通过添加 space =free_x
,您可以确保小节在所有方面具有相同的宽度。
ggplot(sample,aes(category,count))+ geom_bar()+
facet_grid(。〜parent_id,scale =free_x,space =free_x)
I'm making a facet_grid plot using the following approach:
sample <- read.csv('sample.csv')
ggplot(sample, aes(category, count)) + geom_bar() + facet_grid(. ~ parent_id)
My data is structured in the following way:
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
When I make a facet plot of this, it looks like this:
What I would like, is to limit the number of categories on each facet, so that I only show C1, C2, C3
on the first plot (etc). Is there a way that I can limit the number of categories that are shown here?
Here is the output of dput(sample)
and my plot command, so that my image can be easily reproduced:
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)
You can add scales="free_x"
to facet_grid()
. In this case for each facet you will have x values only in range of values used for particular facet. By adding space="free_x"
you can ensure that bars have the same width in all facets.
ggplot(sample, aes(category, count)) + geom_bar() +
facet_grid(. ~ parent_id,scale="free_x",space="free_x")
这篇关于限制构面图中构面的类别数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!