2012-01-16 81 views
1

我有可与所生成的数据帧:řGGPLOT2如何绘制一个汇总统计(例如内)

a <- c(rep("w",3), rep("x",3), rep("y",3), rep("z",3)) 
b <- rnorm(12) 
c <- c(rep(rnorm(1), 3), rep(rnorm(1), 3),rep(rnorm(1), 3),rep(rnorm(1), 3)) 
d <- c(1,1,1,2,2,2,1,1,1,2,2,2) 
test <- data.frame(a,b,c,d) 

我要生成箱图的“B”列,并且在每个箱形图显示c列的值 - 这是每个“a”的一种参考值(因此是重复)。我几乎没有用下面的,这也表明我是多么想要的数据显示(小):

library(ggplot2) 
p <- ggplot(test) 
p <- p + geom_boxplot(aes(a, b)) 
p <- p + geom_point(aes(a, c), colour="red", shape=4, size=4) 
p <- p + facet_grid(. ~ d, , scales="free_x") 
p 

这种方法唯一的问题是,有真正为每3 geom_points(在彼此的顶部) “a”值 - 我如何消除这种低效率?我只想要一个geom_point。

非常感谢

回答

2

我认为你只是想用特有的价值观传递给geom_point一个单独的数据帧:

p <- p + geom_point(data = unique(test[,c('a','c','d')]), 
        aes(a, c), colour="red", shape=4, size=4) 
+0

谢谢 - 现在这个工作很漂亮... – MalteseUnderdog 2012-01-16 13:00:42