简体   繁体   English

在ggplot中绘制置信区间

[英]Plotting confidence intervals in ggplot

I'd like to do the following plot using ggplot: 我想用ggplot做下面的情节:

GgPlot的置信区间

Here is an example of the structure of my df (sort of, draw not to scale with the data): 这是我的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"))

This data frame consists of 16 replicates, each with a given mean and a given standard deviation. 该数据框由16个重复组成,每个重复具有给定的平均值和给定的标准偏差。

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. 此外,我想在同一行中绘制两个不同的重复,但具有不同的着色,按类着色,红色为1级,蓝色为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). 我试着查看这个网站http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/但是无法弄清楚如何为这个结构的任何数据框自动化这个,有X个组(在这种情况下2),K每组重复(在这种情况下,类别1中的8,4,类2和4)。

Is there a good way to do this using ggplot or standard r pkg libraries? 有没有一种使用ggplot或标准r pkg库的好方法?

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 . 我假设您提供的示例数据框架没有以适当的方式构建,因为group1所有值都具有class 1 ,而group2所有值都是类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 ). 所以我创建了新的数据框,还添加了一个名为replicate新列,它显示了每个group的重复次数(四次重复(包含两个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))

Now you can use geom_pointrange() to get points with confidence intervals and facet_wrap() to make plot for each group. 现在,您可以使用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)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM