2

我有可以用箱线图绘制的数据,但每个框的 n 仅为 3。我想在 ggplot2 中使用点范围类型的图来绘制它们。默认情况下,它们相互重叠。当它们在箱线图中分组时,如何将我的点并排分组?

library(ggplot2)

x <- rnorm(12, 3,5) # Real data are not always normally distributed.
y <- c(rep("T1", 6), rep("T2", 6))
z <- rep(c(10,20),6)

dat <- data.frame(Treatment = y, Temp = z, Meas = x)

p <- ggplot(dat, aes(Treatment, Meas))
p + geom_boxplot(aes(fill=factor(Temp)))

编辑:我更新了问题以按照建议排除自举(最初的想法是使用置信区间作为误差线。一个问题的问题太多=D)。此处给出了更详细的引导问题

在此处输入图像描述

4

1 回答 1

5

You have two questions (try to avoid that).

  1. Bootstrapping. How do you bootstrap from a sample of 3 points, where you don't know the underlying distribution?

  2. Line ranges. I've used your original data to construct line ranges. For a line range, you just need a min, max and middle value:

    ##First rearrange your data frame
    dat = with(dat, dat[order(Treatment, Temp, Meas),])
    dat$type = c("min", "mid", "max")
    
    library(reshape2)
    dat1 = dcast(dat, Treatment + Temp ~  type, value.var = "Meas")
    

Then plot as usual:

    p = ggplot(dat1) +
        geom_pointrange(aes(ymin=min, ymax=max, 
                            y=mid,x=Treatment, group=Temp),
           position=position_dodge(width=0.20)) 

The position arguments stops the lines being placed on top of each other. This gives:

Example line range plot

于 2012-04-26T11:15:04.220 回答