r - 在ggplot2中使用stat_summary计算平均值和sd,然后连接误差线的平均值

标签 r plot ggplot2

在以下示例中,均值和se是从原始数据中计算出来的,并以barplot绘制。我想做同样的事情,但我不想使用barplot,而是要使用连接点。所以,如果有人可以告诉我如何...我将非常感激...谢谢

例:

     data(ToothGrowth)

     ToothGrowth$F3 <- letters[1:2]
     # coerce dose to a factor
     ToothGrowth$dose <- factor(ToothGrowth$dose, levels = c(0.5,1,2))
     # facetting on the third factor
     ggplot(ToothGrowth, aes(y = len, x = supp )) + 
     stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, geom = 'bar', 
     aes(fill =dose), position = 'dodge') +
     stat_summary(fun.ymin = function(x) mean(x) - sd(x), 
           fun.ymax = function(x) mean(x) + sd(x), position ='dodge', 
           geom = 'errorbar', aes(group = dose))+
    facet_wrap(~F3)   

最佳答案

您可以将geom pointrange用于指示均值和误差线的两个点。

ggplot(ToothGrowth, aes(y = len, x = supp, colour = dose, group = dose)) + 
  stat_summary(fun.y = mean,
               fun.ymin = function(x) mean(x) - sd(x), 
               fun.ymax = function(x) mean(x) + sd(x), 
               geom = "pointrange") +
  stat_summary(fun.y = mean,
               geom = "line") +
  facet_wrap( ~ F3)

关于r - 在ggplot2中使用stat_summary计算平均值和sd,然后连接误差线的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239418/

相关文章:

r - 在 R 中并排绘制几组箱线图

r - 如何将基于日期的颜色渐变应用于 ggplot2 散点图?

r - R:返回具有多个列的data.frame的pmin或pmax

3d - 在 gnuplot 中在 pm3d 表面绘制点

r - 让knitr运行r脚本: do I use read_chunk or source?

r - 强制 R 基本图以刻度线结束

从 ggplot 对象返回调用

r - 在使用 R、Sweave 和 knitr 的动态报告中使用的绘图中的 Unicode 字符

r - 如何在 Rstudio 启动时停止加载包

r - 将 NA 添加到列表中