4

我想使用 ggplot 进行以下绘图:

按组划分的置信区间 GgPlot

这是我的 df 结构的示例(有点,绘制不与数据按比例绘制):

example.df = data.frame(mean = c(0.3,0.8,0.4,0.65,0.28,0.91,0.35,0.61,0.32,0.94,0.1,0.9,0.13,0.85,0.7,1.3), 
                            std.dev = c(0.01,0.03,0.023,0.031,0.01,0.012,0.015,0.021,0.21,0.13,0.023,0.051,0.07,0.012,0.025,0.058),
                            class = c("1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"),
                            group = c("group1","group2","group1","group2","group1","group2","group1","group2","group1","group2","group1","group2","group1","group2","group1","group2"))

该数据框由 16 个重复项组成,每个重复项具有给定的平均值和给定的标准偏差。

对于每个重复,我想绘制置信区间,其中我的图形示例中的大点是平均估计值,条形的长度是标准偏差的两倍。

另外我想在同一行绘制两个不同的复制品,但颜色不同,按类别着色,红色是 1 类,蓝色是 2 类。

最后,我想将整个情节分成两个面板(在同一行),对应于两个不同的组。

我试着查看这个网站,http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_ (ggplot2)/但无法弄清楚如何为这个结构的任何数据框自动化这个,有X个组(在这种情况下为 2),并且每组重复 K 次(在这种情况下为 8、4 类 1 和 4 类 2)。

有没有使用 ggplot 或标准 r pkg 库的好方法?

4

1 回答 1

6

我想您提供的示例数据框没有以适当的方式构建,因为group1haveclass 1中的所有值group2都是 class 2。所以我制作了新的数据框,还添加了名为replicate显示复制数的新列(每个复制四个复制(有两个class值)group)。

example.df = data.frame(mean = c(0.3,0.8,0.4,0.65,0.28,0.91,0.35,0.61,0.32,0.94,0.1,
                                0.9,0.13,0.85,0.7,1.3), 
                        std.dev = c(0.01,0.03,0.023,0.031,0.01,0.012,0.015,0.021,0.21,
                                  0.13,0.023,0.051,0.07,0.012,0.025,0.058),
                        class = c("1","2","1","2","1","2","1","2","1","2","1",
                                 "2","1","2","1","2"),
                        group = rep(c("group1","group2"),each=8),
                        replicate=rep(rep(1:4,each=2),time=2))

现在您可以使用geom_pointrange()来获取具有置信区间的点并facet_wrap()为每个组绘制图。

ggplot(example.df,aes(factor(replicate),
               y=mean,ymin=mean-2*std.dev,ymax=mean+2*std.dev,color=factor(class)))+
  geom_pointrange()+facet_wrap(~group)

在此处输入图像描述

于 2013-10-26T12:01:14.793 回答