我的数据如下所示:
df.plot <-structure(list(color = structure(c(2L, 2L, 3L, 1L, 3L, 4L, 3L,
1L, 4L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L,
3L, 3L, 3L, 3L), .Label = c("54", "55", "61", "69"), class = "factor"),
date = structure(c(16687, 16687, 16687, 16687, 16687, 16687,
16688, 16688, 16688, 16689, 16689, 16690, 16693, 16693, 16693,
16694, 16694, 16695, 16695, 16695, 16695, 16696, 16696, 16696,
16696, 16696, 16696), class = "Date"), facet = c("A",
"A", "A", "A", "A", "B",
"B", "A", "B", "B", "B", "B",
"B", "B", "B", "B", "A", "B",
"A", "B", "A", "C", "B", "C",
"C", "B", "C")), class = "data.frame", row.names = c(NA,
-27L), .Names = c("color", "date", "facet"))
下表总结了每个面/颜色的观察次数
df.plot %>% group_by(facet, color) %>% summarise(n())
---------------------
facet color n()
------- ------- -----
A 54 5
A 55 2
A 61 2
B 54 6
B 55 3
B 61 2
B 69 3
C 61 4
---------------------
ggplot 生成此图:
ggplot(df.plot, aes(x=date, fill=color)) +
geom_dotplot(binwidth=1) +
coord_fixed(ratio=1) +
ylim(0,7) +
facet_grid(facet ~ .)
为什么点数不对?例如,我希望 A 54 有 5 个点,而该图仅显示 4 个。
最佳答案
不同颜色但相同日期的点重叠。添加 stackgroups=TRUE
应该会给出预期的结果:
ggplot(df.plot, aes(x=date, fill=color)) +
geom_dotplot(binwidth=1, stackgroups=TRUE) +
coord_fixed(ratio=1) +
ylim(0,7) +
facet_grid(facet ~ .)
关于r - geom_dotplot 的点数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36178379/