2012-09-13 52 views

回答

4

正如@smillig提到的,您可以使用此实现GGPLOT2。下面的代码重现了你非常好的情节 - 警告它非常棘手。首先加载GGPLOT2包,并产生了一些数据:

library(ggplot2) 
dd = data.frame(values=runif(21), type = c("Control", "Treated", "Treated + A")) 

下更改默认的主题:

theme_set(theme_bw()) 

现在我们所建立的情节。

  1. 构造一个基本对象 - 没有被绘制:

    g = ggplot(dd, aes(type, values)) 
    
  2. 添加上的点:

    g = g + geom_jitter(aes(pch=type), position=position_jitter(width=0.1)) 
    
  3. 添加于:根据类型调整默认抖动和变化字形“盒子”:计算盒子的结束位置。在这种情况下,我选择了平均值。如果你不想要这个盒子,只需要省略这一步。

    g = g + stat_summary(fun.y = function(i) mean(i), 
         geom="bar", fill="white", colour="black") 
    
  4. 添加上一些误差条:计算所述上/下限和调节的条宽:

    g = g + stat_summary(
         fun.ymax=function(i) mean(i) + qt(0.975, length(i))*sd(i)/length(i), 
         fun.ymin=function(i) mean(i) - qt(0.975, length(i)) *sd(i)/length(i), 
         geom="errorbar", width=0.2) 
    
  5. 显示的情节

    g 
    

enter image description here

  1. 在我上面的R代码中,我使用stat_summary来计算实时需要的值。您也可以创建单独的数据帧并使用geom_errorbargeom_bar
  2. 要使用base R,看看我对这个question的回答。
+0

谢谢csgillespie,你的2号链接实际上给了我几乎所需的东西。但两者都是很好的解决方案我调整了一些供我自己使用。 – crazian

+0

这样做的目的是为了能够在一个情节框架中显示两组的中位数,以及数据点和异常值。 这里是代码: – crazian

+0

https://gist.github.com/9bfb05dcecac3ecb7491 – crazian

3

如果你不介意使用ggplot2包,有一个简单的方法,使类似的图形与geom_boxplotgeom_jitter。使用mtcars示例数据:

library(ggplot2) 
p <- ggplot(mtcars, aes(factor(cyl), mpg)) 
p + geom_boxplot() + geom_jitter() + theme_bw() 

产生如下图:

enter image description here

的文档可以在这里看到:http://had.co.nz/ggplot2/geom_boxplot.html