创建包含平均值和平均值标准误差的散点图

发布于 2025-01-15 02:21:03 字数 196 浏览 3 评论 0原文

我正在尝试使用 ggplot2 创建散点图。除了各个值之外,我还想显示每组的平均值以及平均值的标准误差。您可以在下面找到我使用 GraphPad Prism 创建的所需结果的示例。

我提前非常感谢您抽出宝贵的时间! BR

GraphPad 中的绘图

I am trying to create a scatterplot using ggplot2. Besides the individual values, I want to display the mean for each group and the standard error of the mean. Below you can find an example of the desired result which I created using GraphPad Prism.

I thank you very much in advance for your precious time!
BR

plot in GraphPad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2025-01-22 02:21:03

以下代码很好地复制了您的情节:

library(ggplot2)

ggplot(df, aes(x = group, y = y, group = group)) + 
  geom_point(aes(shape = group), size = 3,
             position = position_jitter(width = 0.1)) +
  stat_summary(fun = mean, 
               fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), 
               fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)),
               geom = 'errorbar',  width = 0.25) +
  stat_summary(fun = mean, fun.min = mean, fun.max = mean,
               geom = 'errorbar',  width = 0.5) +
  scale_y_continuous(limits = c(0, 30)) +
  scale_shape_manual(values = c(16, 15)) +
  labs(x = '', y = '') +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        legend.position = 'none')

在此处输入图像描述

您的数据需要采用以下格式:

df <- data.frame(group = rep(c("A", "B"), times = c(8, 4)),
                 y = c(1.8, 2.5, 2.4, 4, 5.5, 13, 14, 26,
                       2.3, 2.8, 5.5, 10))

df
#>    group    y
#> 1      A  1.8
#> 2      A  2.5
#> 3      A  2.4
#> 4      A  4.0
#> 5      A  5.5
#> 6      A 13.0
#> 7      A 14.0
#> 8      A 26.0
#> 9      B  2.3
#> 10     B  2.8
#> 11     B  5.5
#> 12     B 10.0

The following code replicates your plot fairly well:

library(ggplot2)

ggplot(df, aes(x = group, y = y, group = group)) + 
  geom_point(aes(shape = group), size = 3,
             position = position_jitter(width = 0.1)) +
  stat_summary(fun = mean, 
               fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), 
               fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)),
               geom = 'errorbar',  width = 0.25) +
  stat_summary(fun = mean, fun.min = mean, fun.max = mean,
               geom = 'errorbar',  width = 0.5) +
  scale_y_continuous(limits = c(0, 30)) +
  scale_shape_manual(values = c(16, 15)) +
  labs(x = '', y = '') +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        legend.position = 'none')

enter image description here

Your data needs to be in the following format:

df <- data.frame(group = rep(c("A", "B"), times = c(8, 4)),
                 y = c(1.8, 2.5, 2.4, 4, 5.5, 13, 14, 26,
                       2.3, 2.8, 5.5, 10))

df
#>    group    y
#> 1      A  1.8
#> 2      A  2.5
#> 3      A  2.4
#> 4      A  4.0
#> 5      A  5.5
#> 6      A 13.0
#> 7      A 14.0
#> 8      A 26.0
#> 9      B  2.3
#> 10     B  2.8
#> 11     B  5.5
#> 12     B 10.0
遇到 2025-01-22 02:21:03

看一下 ggplot2 中的 geom_errorbar() 函数。您应该能够使用 geom_errorbar()geom_dotplot() 重现与图中类似的绘图。

Have a look at the geom_errorbar() function in ggplot2. You should be able to reproduce a plot similar to that in figure using geom_errorbar() and geom_dotplot().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文