我有一个几乎箱线图像抖动情节:

dt <- rbind(se,cb,cb.se)
qplot(ds, size, data=dt, geom="jitter", colour=root, facets = test ~ .)

“情节”

我喜欢把一个汇总标签为每个组中的情节中 - 例如大小总计在这里:

 aggregate(list(size=dt$size), list(dt$ds, dt$test), sum)

   Group.1  Group.2   size
1     b217       se   9847
2      c10       se  97296
3     c613       se  21633
4       c7       se 207540
...

我用+ geom_text(aes(x=ds, y=128, label=sum(size)), size=2)添加标签试过,但我得到的各位置相同的标签 - ?我怎么能得到的只是数据的部分的总和

修改 下面是我在哪里了 - 也许我只是在错误的方向前进

data <- rbind(se,cb,cb.se)
labels <-ddply(data, c("ds", "test"), function(df) sum(df$size))
ggplot(data=data, aes(x=ds)) +
  geom_jitter(aes(y=size, colour=root)) +
  geom_text(data=labels, aes(x=ds, y=600, label=V1), size=3) +
  facet_wrap(test ~ .)

此代码不能正常工作 - 我得到一个错误undefined columns selected ...某处。也许是因为多data=部分?

有帮助吗?

解决方案

既然你不提供采样数据,我将使用随机数据证明的溶液中。

set.seed(1)
n <- 100
dat <- data.frame(
    ds = sample(paste("x", 1:8, sep=""), n, replace=TRUE),
    size = runif(n, 0, 250),
    root = sample(c(TRUE, FALSE), n, replace=TRUE),
    test = sample(c("se", "cb", "cb.se"), n, replace=TRUE) 
)


head(dat)
  ds      size  root  test
1 x3 163.68098  TRUE cb.se
2 x3  88.29932  TRUE    se
3 x5  67.56504 FALSE    cb
4 x8 248.17102  TRUE    cb
5 x2 158.37332  TRUE    cb
6 x8  53.30203 FALSE cb.se

p <- ggplot(dat, aes(x=ds, y=size)) + 
  geom_jitter(aes(colour=root)) + 
  facet_grid(test~.) 

创建包含标签数据的数据帧。注意使用summarize的。这告诉ddply创建一个新的列到data.frame

labels <- ddply(dat, .(ds, test), summarize, size=round(sum(size), 0))
head(labels)
  ds  test size
1 x1    cb  193
2 x1 cb.se  615
3 x1    se  274
4 x2    cb  272
5 x2 cb.se  341
6 x2    se 1012

p + geom_text(aes(x=ds, label=size, y=128), data=labels, size=2) 

“在这里输入的图像描述”

其他提示

在这里看看。这可能是有益的 直接添加标签到GGPLOT2和晶格地块

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top