在ggplot中绘制置信区间 [英] Plotting confidence intervals in ggplot

查看:1896
本文介绍了在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 , 组1, 组2, 组1, 组2, 组1, 组2, 组1, 组2, 组1, 组2, 组1, 组2, group1,group2))

这个数据框由16个重复组成,每个重复有一个给定的均值和一个给定的标准差。

对于eac h复制我想绘制置信区间,其中图例中的大点是平均估计值,并且条的长度是标准偏差的两倍。



我还想在同一行中绘制两个不同的复制品,但着色不同,按类别着色,红色是1级,蓝色是2级。 / p>

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

我试图寻找此网站, http:// www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ ,但无法弄清楚如何对这个结构的任何数据框自动执行此操作,并将X个组(本例中为2)和K复制每组(在这种情况下,第1类中的第8,4类和第2类中的第4类)。

有没有一种使用ggplot或标准r pkg图书馆?

解决方案

我想你提供的示例数据框不是以适当的方式构建的,因为所有值在 group1 中有 class 1 ,并在 group2 全都是类 2 。所以我创建了新的数据框,并添加了一个名为 replicate 的新列,它显示了复制次数(四次复制(有两个 class 值)在每个)。

  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)


I'd like to do the following plot using ggplot:

Here is an example of the structure of my df (sort of, draw not to scale with the data):

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"))

This data frame consists of 16 replicates, each with a given mean and a given standard deviation.

For each replicate I'd like to plot the confidence intervals, where the big dot in my figure example is the mean estimate, and the length of the bar is twice the standard deviation.

Also I'd like to plot two different replicates in the same line but with different coloring, coloring it by class, red is class 1 and blue is class 2.

Finally, I'd like to divide the whole plot into two panels (in the same row) corresponding to the two different groups.

I tried looking into this site, http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ but couldn't figure out how to automate this for any data frame of this structure, with X number of groups (in this case 2), and K replicates per group (in this case 8, 4 of class 1 and 4 of class 2).

Is there a good way to do this using ggplot or standard r pkg libraries?

解决方案

I suppose that sample data frame you provided isn't build in appropriate way because all values in group1 have class 1, and in group2 all are class 2. So I made new data frame, added also new column named replicate that shows number of replicate (four replicates (with two class values) in each 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))

Now you can use geom_pointrange() to get points with confidence intervals and facet_wrap() to make plot for each group.

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)

这篇关于在ggplot中绘制置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆