如何在R中使用gg Betweenstats对不同时间点的分组数据?

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

我正在尝试使用 R 版本 4.3.2 中

ggbetweenstats
包中的
ggstatsplot
函数来创建一个图,显示 3 个不同时间点的 2 个不同治疗组之间的一些数据的分布。

到目前为止我发现的是

grouped_ggbetweenstats
函数,它对治疗组而不是单个图产生
facet_wrap
型效应。我可以通过
geom_boxplot
使用
ggplot2
并在
ggsignif
包中包含统计信息,但我真的很喜欢
ggbetweenstats
制作的图。

这是我的数据:

Treatment <- sample(c("A", "B"), 100, replace=T) 
Timepoint <- sample(c("1", "2", "3"), 100, replace=T) 
Value <- sample(20:80, size=100, replace=T) 
df <- data.frame(Treatment, Timepoint, Value) 
library(ggstatsplot)

到目前为止我所尝试的,产生了两个单独的图:

grouped_ggbetweenstats(data=df, x=Timepoint, y=Value,
                       grouping.var=Treatment, results.subtitle=F) 

Plot1

我想创建什么:

library(ggplot2)

ggplot() + 
  geom_boxplot(data=df, aes(x=Timepoint, y=Value, color=Treatment)) 

Plot2

有没有办法使用

ggstatsplot
包来创建这种类型的绘图,或者我需要组合
geom_boxplot
geom_point
geom_violin
geom_signif
等?

r ggplot2 visualization
1个回答
0
投票

我认为

ggstatsplot
确实有一个漂亮的外观,但与许多旨在轻松产生专业结果的扩展包一样,它可能更难哄骗它为您提供更多定制输出。如果你想添加
ggsignif
比较,使用纯 ggplot 复制外观并不是非常困难:

library(ggplot2)

ggplot(df, aes(Timepoint, Value, group = interaction(Timepoint, Treatment))) +
  geom_point(aes(color = Treatment), 
             position = position_jitterdodge(dodge.width = 0.9, 0.1),
             size = 3, alpha = 0.3) +
  geom_boxplot(fill = NA, color = "black", width = 0.3, 
               position = position_dodge(0.9)) +
  geom_violin(fill = NA, color = "black", width = 0.8,
              position = position_dodge(0.9)) +
  geom_point(stat = "summary", size = 5, color = "#8a0f00",
             position = position_dodge(0.9), fun = mean) +
  scale_color_brewer(palette = "Set2") +
  theme_minimal()

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