带置信区间的雨云图(平均值)

问题描述 投票:0回答:1

我正在尝试创建一个具有 95% 置信区间的雨云图,但是我的代码继续生成一个具有箱线图间隔的雨云图。意思是黑色圆圈是中位数而不是平均值。我如何更改我的代码以添加错误栏以实现此目的?

这是我尝试创建雨云图的两个代码,但没有均值。我附上了情节的样子。最后一张图片是我想要的情节。What I want it to look like here Image 3

Image 1

df %>%ggplot(aes(x=thresh.x, y=sex, fill=sex))+stat_slab(aes(thickness = stat(pdf*n)),scale = 0.7) +stat_dotsinterval(side = "bottom",scale = 0.7,slab_size = NA)

Image 2

df %>%
  ggplot(aes(x=thresh.x, y=sex, fill=sex))+
  stat_slab(aes(thickness = stat(pdf*n)), 
                scale = 0.7) +
  stat_dotsinterval(side = "bottom",
                    scale = 0.7,
                    slab_size = NA) + 
  scale_fill_brewer(palette = "Set1") +
  theme(legend.position = "top")+
  scale_x_continuous(limits = c(-4.5, .5), breaks = seq(-4.5, 0.5, by = 0.5))+
  labs(title="Raincloud plot with ggdist")

可以在这里找到: https://z3tt.github.io/Rainclouds/

ggplot(iris, aes(Species, Sepal.Width)) + 
  ggdist::stat_halfeye(adjust = .5, width = .3, .width = c(0.5, 1)) + 
  ggdist::stat_dots(side = "left", dotsize = .4, justification = 1.05, binwidth = .1)
r plot confidence-interval violin-plot ggdist
1个回答
1
投票

感谢您用最小的可重现示例更新您的问题。一种潜在的“简单”解决方案是将平均值添加到现有图:

library(tidyverse)
library(ggdist)

iris %>%
  ggplot(aes(x=Petal.Length, y=Species))+
  stat_slab(aes(fill=Species), scale = 0.7) +
  stat_dotsinterval(aes(fill=Species),
                    side = "bottom",
                    scale = 0.7,
                    slab_size = NA) + 
  scale_fill_brewer(palette = "Set1") +
  stat_summary(aes(color = "mean"), 
               fun=mean, geom="point", shape="|", size=5) +
  theme_bw(base_size = 16) +
  theme(legend.position = "top")+
# scale_x_continuous(limits = c(-4.5, .5), breaks = seq(-4.5, 0.5, by = 0.5))+
  labs(title="Raincloud plot with ggdist") +
  scale_color_manual(name = "",
                     values = c("mean" = "cyan"))

创建于 2023-03-02 与 reprex v2.0.2

如果这不适合,我怀疑你需要自己计算统计数据,例如

iris %>%
  group_by(Species) %>%
  mutate(mean = mean(Petal.Length),
         se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
  ungroup() %>%
  ggplot(aes(x=Petal.Length, y=Species)) +
  stat_slab(aes(fill = Species)) +
  stat_dots(side = "bottom", shape = 16) + 
  scale_fill_brewer(palette = "Set1") +
  geom_errorbar(aes(xmin = mean - 1.96 * se,
                    xmax = mean + 1.96 * se), width = 0.2) +
  stat_summary(fun=mean, geom="point", shape=16, size=2.5) +
  theme_bw(base_size = 16) +
  theme(legend.position = "top")+
  # scale_x_continuous(limits = c(-4.5, .5), breaks = seq(-4.5, 0.5, by = 0.5))+
  labs(title="Raincloud plot with ggdist")

创建于 2023-03-02 与 reprex v2.0.2

这能解决您的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.