【发布时间】:2019-07-19 09:47:13
【问题描述】:
如何使用 ggplot 创建集群的分组图,每个集群中都有几个人。
例如:
df <- structure(list(ID = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
cluster = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"),
val = c(1.2581800436601, 6.79055672604591, 9.77732860250399,
3.60806297743693, 1.14399523707107, 7.9990872181952, 3.16242988454178,
5.64627967076376, 8.82345798192546, 4.29119206266478, 8.62997844815254,
6.46683012368158), date = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
1, 2)), class = "data.frame", row.names = c(NA, -12L))
ID cluster val date
1 1 1 1.258180 1
2 1 1 6.790557 2
3 2 1 9.777329 1
4 2 1 3.608063 2
5 3 2 1.143995 1
6 3 2 7.999087 2
7 4 2 3.162430 1
8 4 2 5.646280 2
9 5 3 8.823458 1
10 5 3 4.291192 2
11 6 3 8.629978 1
12 6 3 6.466830 2
我想创建 2x3 图,包含 3 列集群,每个人的每列有 2 行图。
我正在使用的当前代码给出了情节的一般结构,但有很多空白情节:
ggplot(df, aes(date,val)) + geom_bar(stat='identity') + facet_grid(ID~cluster)
【问题讨论】:
-
facet_wrap(~ cluster + ID) -
@PoGibas:不,因为第一行将包含集群/ID 1/1、1/2 和 2/3,第二行将包含 2/4、3/5 和 3/6。
标签: r ggplot2 plot facet-wrap