r - 将统计测试与 rstatix 和每个类别的颜色填充相结合
问题描述
我用这段代码用 ggplot2 做小提琴图:
Cells1 %>%
ggplot(aes(x=IR_time, y=AreaShape_Area, fill=IR_time)) +
geom_violin(col=NA) +
guides(fill=FALSE) +
stat_summary(fun.data=data_summary, col = "black") +
theme_gray() +
ggtitle("Cell area after irradiation (3Gy)") +
ylab("\nArea (pixels)") +
xlab("\nDays after exposure to 3Gy\n") +
scale_fill_manual(values=wes_palette(n=5, name="Moonrise3")) +
theme(
plot.title = element_text(size=16),
axis.title = element_text(size=12, face="bold"),
axis.text = element_text(size=12))
这产生了这个情节:
现在我想执行统计检验(这里是成对 t 检验)并将结果添加到图上。我使用 rstatix 包。所以这是新的代码:
stat.test <- Cells1 %>% pairwise_t_test(AreaShape_Area ~ IR_time, pool.sd=FALSE, p.adjust.method="bonferroni", ref.group="0") %>%
add_y_position()
Cells1 %>%
ggplot(aes(x=IR_time, y=AreaShape_Area, fill=IR_time)) +
geom_violin(col=NA) +
guides(fill=FALSE) +
stat_summary(fun.data=data_summary, col = "black") +
theme_gray() +
ggtitle("Cell area after irradiation (3Gy)") +
ylab("\nArea (pixels)") +
xlab("\nDays after exposure to 3Gy\n") +
scale_fill_manual(values=wes_palette(n=5, name="Moonrise3")) +
theme(
plot.title = element_text(size=16),
axis.title = element_text(size=12, face="bold"),
axis.text = element_text(size=12)) +
stat_pvalue_manual(stat.test)
但这会导致此错误:
错误:美学必须是长度1或与数据相同(4):填充
显然,这来自 aes() 中的 fill=IR_time 参数。如果我用 fill="blue" 替换它,它可以工作,我没有错误消息。但我想根据 IR_time 和绘图上的统计数据同时拥有两种颜色。
你知道我该如何解决吗?
解决方案
我创建了一个大致相似的同名数据集:
set.seed(69)
Cells1 <- data.frame(
IR_time = factor(rep(c(0, 2, 4, 8, 12), each = 1000)),
AreaShape_Area = rgamma(5e3, rep((2:6)^1.8, each = 1e3)) * 3e3)
您的代码需要一个名为 的函数data_summary
,您没有包含该函数,并且似乎不在我可以在 Google 上找到的任何常见 R 包中。我猜它是这样的:
data_summary <- function(x) {
data.frame(y = mean(x), ymin = mean(x) - sd(x), ymax = mean(x) + sd(x))
}
您没有包含您正在使用的软件包,因此需要进行一些侦探工作才能确定我们需要:
library(ggplot2)
library(wesanderson)
library(rstatix)
library(ggpubr)
现在我们可以运行您的代码了。stat_pvalue_manual(stat.test)
据我所知,如果将行更改为,这应该很容易解决stat_pvalue_manual(data = stat.test, inherit.aes = FALSE)
:
stat.test <- Cells1 %>%
pairwise_t_test(AreaShape_Area ~ IR_time,
pool.sd = FALSE,
p.adjust.method = "bonferroni",
ref.group = "0") %>%
add_y_position()
Cells1 %>%
ggplot(aes(x = IR_time, y = AreaShape_Area, fill = IR_time)) +
geom_violin(col = NA) +
guides(fill = FALSE) +
stat_summary(fun.data = data_summary, col = "black") +
theme_gray() +
ggtitle("Cell area after irradiation (3Gy)") +
ylab("\nArea (pixels)") +
xlab("\nDays after exposure to 3Gy\n") +
scale_fill_manual(values = wes_palette(n = 5, name = "Moonrise3")) +
theme(
plot.title = element_text(size = 16),
axis.title = element_text(size = 12, face = "bold"),
axis.text = element_text(size = 12)) +
stat_pvalue_manual(data = stat.test, inherit.aes = FALSE)
由reprex 包(v0.3.0)于 2020 年 9 月 30 日创建
推荐阅读
- amazon-web-services - 在许多 lambda 函数之间共享“初始化阶段”
- ocaml - 从 OCaml 中的一些 x 中获取 x
- linux - 在概念上将用户虚拟地址、内核逻辑地址和内核虚拟地址捆绑在一起
- android - 带有 Workmanager 的 Flutter Provider
- python - 返回 True 以比较列表中的最后两个元素(循环) - Python
- amazon-web-services - 无法连接到端点 URL:“https://api.ecr-public.xxxxxxxxx.amazonaws.com/”
- node.js - _.filter() 和 nodejs 中的新函数
- geodjango - Geodjango 和几何
- reactjs - 如何调查基于 React 和 AWS 的应用程序中的网站缓存?
- c# - 如何使用 EntityFrameworkCore.TemporalTables.Extensions