如何在ggplot2中按组获取彩色点图?

时间:2015-09-29 09:44:59

标签: r ggplot2

我无法使用这些数据对分组点图进行着色(奇怪的是,我无法使用ggplot2附带的其中一个数据集重现该问题,因此我为额外步骤道歉。

data <- data.frame(
  Period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L),
  Rate = c(0, 10, 10, 100, 20, 10, 20, 10, 5, 100, 0, 50, 50, 100, 20, 100, 0, 5, 5, 0, 0, 50, 10, 100, 15, 50, 0, 0, 5, 0, 100, 50, 0, 100, 0, 50, 0, 0, 5, 0, 100, 100, 50, 100, 0, 0, 0, 0, 5, 100, 100, 0, 0, 100, 0, 0, 0, 0, 0, 0, 100, 0, 0, 100, 0, 100, 0, 0, 100, 10, 100, 0, 0, 100, 0, 10, 5, 0, 100, 0, 100, 0, 100, 100, 0, 10, 5, 0, 0, 100, 100, 0, 100, 100, 30, 10, 10, 100, 0, 100, 100, 0, 0, 0, 10, 10, 0, 10, 100, 50, 0, 0, 100, 0, 0, 10, 20, 100, 100, 100, 0, 60, 0, 0, 0, 100, 50, 100, 100, 100, 100, 100, 20, 0, 0, 20, 100, 10, 0, 0, 100, 100, 30, 0, 0, 10, 100, 0, 0, 0),
  Subject = c("1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6")
)
g <- ggplot(data = data, mapping = aes(x = factor(Period), y = Rate, fill = Subject, group = Period))
g + geom_dotplot(binwidth = 1, binaxis = "y", stackdir = "center")

dotplot

这很好,除了奇怪的是,点不会保留fill=Subject颜色

如何按Subject为它们着色?

更新

没有 group = Period,在评论中建议,填充颜色保留,但没有真正的点图 - 所有点都重叠。

nongrouped

2 个答案:

答案 0 :(得分:2)

EDIT2 此问题似乎也得到了解决here。我已经将这个问题标记为副本。

直接在fill中使用geom_dotplot似乎有效:

data$Period <- as.factor(data$Period)
g <- ggplot(data, aes(x = Period, y = Rate, fill = Subject, group = Period))
g + geom_dotplot(binwidth = 1, binaxis = "y", stackdir = "center", 
                 fill = data$Subject)

image

显然,fill中的ggplot可能已经过时了。

编辑

添加position = "dodge"并省略group = Period也会产生类似的情节:

ggplot(data, aes(x = factor(Period), y = Rate, fill = factor(Subject))) +
  geom_dotplot(binaxis = "y", stackdir = "center", position = "dodge", binwidth = 2)

image2

这是吗?

答案 1 :(得分:1)

尝试geom_jitter

g + geom_jitter(binwidth = 1, binaxis = "y", stackdir = "center", aes(group = Subject, color = Subject), size = 4)

enter image description here