ggplot2:为每组平均添加行

时间:2010-11-22 09:54:21

标签: r ggplot2

library(ggplot2)

orderX <- c("A" = 1, "B" = 2, "C" = 3)
y <- rnorm(20)
x <- as.character(1:20)
group <- c(rep("A", 5), rep("B", 7), rep("C", 5), rep("A", 3))
df <- data.frame(x, y, group)
df$lvls <- as.numeric(orderX[df$group])

ggplot(data = df, aes(x=reorder(df$x, df$lvls), y=y)) + 
geom_point(aes(colour = group)) + 
geom_line(stat = "hline", yintercept = "mean", aes(colour = group))

我想创建一个这样的图形: graph with averages for each group

当我不需要重新排序X的值时,这确实有效,但是,当我使用重新排序时,它不再起作用。

2 个答案:

答案 0 :(得分:17)

根据您的问题,我不会df$x与您的数据完全相关,尤其是如果您可以重新订购它。如何将group用作x,并jitter实际x位置来分隔点:

ggplot(data=df, aes(x=group,y=y,color=group)) + geom_point() +
geom_jitter(position = position_jitter(width = 0.4)) +
geom_errorbar(stat = "hline", yintercept = "mean",
  width=0.8,aes(ymax=..y..,ymin=..y..))

我使用了errorbar而不是h_line(并将ymax和ymin折叠为y)因为hline很复杂。如果有人对这部分有更好的解决方案,我很乐意看到。

alt text


<强>更新

如果您想保留X的顺序,请尝试此解决方案(使用修改后的X)

df$x = factor(df$x)

ggplot(data = df, aes(x, y, group=group)) + 
facet_grid(.~group,space="free",scales="free_x") + 
geom_point() + 
geom_line(stat = "hline", yintercept = "mean")

alt text

答案 1 :(得分:4)

从ggplot2 2.x开始,这种方法不幸被打破了。

以下代码提供了我想要的内容,预先提供了一些额外的计算:

library(ggplot2)
library(data.table)

orderX <- c("A" = 1, "B" = 2, "C" = 3)
y <- rnorm(20)
x <- as.character(1:20)
group <- c(rep("A", 5), rep("B", 7), rep("C", 5), rep("A", 3))
dt <- data.table(x, y, group)
dt[, lvls := as.numeric(orderX[group])]
dt[, average := mean(y), by = group]
dt[, x := reorder(x, lvls)]
dt[, xbegin := names(which(attr(dt$x, "scores") == unique(lvls)))[1], by = group]
dt[, xend := names(which(attr(dt$x, "scores") == unique(lvls)))[length(x)], by = group]

ggplot(data = dt, aes(x=x, y=y)) + 
    geom_point(aes(colour = group)) +
    facet_grid(.~group,space="free",scales="free_x") + 
    geom_segment(aes(x = xbegin, xend = xend, y = average, yend = average, group = group, colour = group))

结果图片:

enter image description here