r - stat_compare_means 条件 p 值标签导致 ggplot 填充失败
问题描述
按照这篇文章的示例,我的目标是仅在 p 值低于某个阈值时标记比较。在终于让它工作之后,我看到它正在抵消之前设置的 ggplot 参数并且不再能够找到填充变量。
我已经包含了一个可重现的示例和我想要的输出。
set.seed(20)
col1<-c(rep('E', each = 8))
col2<-c(rep('R', each = 8))
col3<-c(rep('S', each = 8))
behaviour<-c(col1,col2,col3)
value <- runif(length(behaviour), min=2, max=8)
shannon <- c(rep('Shannon', each = length(behaviour)))
test.data <- data.frame(behaviour, value, shannon)
d <- compare_means(value~behaviour, data = test.data,method = 't.test')
d %<>% mutate(y_pos = c(5,5.5,6),labels = ifelse(p<0.17,p.format,p.signif))
d
.y. group1 group2 p p.adj p.format p.signif method y_pos labels
value E R 0.4678791 0.76 0.47 ns T-test 5.0 ns
value E S 0.1559682 0.47 0.16 ns T-test 5.5 0.16
value R S 0.3794209 0.76 0.38 ns T-test 6.0 ns
shannonviolin<-ggplot(test.data, aes(x = behaviour, y = value)) +
labs(y= "Shannon",color = " ",size=8)+
geom_violin(position=position_dodge(width=1.2),alpha = 0.7,scale="width",adjust=0.9,trim=FALSE)+
geom_boxplot(width=0.4,outlier.colour = "transparent")+
geom_point(aes(fill = behaviour), size = 5, shape = 21, position = position_jitterdodge()) +
theme_classic()+
scale_fill_manual(values = c("E" = '#3797a4', "R"= '#96bb7c',"S"= '#944e6c'))+
theme(text = element_text(size = 18),
axis.title.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
legend.position = "right",
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title = element_blank()
)+
ylim(0, 14)
shannonviolin+geom_signif(data = as.data.frame(d), tip_length = 0.01, aes(xmin=group1, xmax=group2, annotations=labels,y_position=c(12.4,13,13.6)),manual=TRUE)
这是让它工作:
但是没有填充颜色。如果我在ggplot中指定填充参数:
shannonviolinfill<-ggplot(test.data, aes(x = behaviour, y = value,fill=behaviour)) +
labs(y= "Shannon",color = " ",size=8)+
geom_violin(position=position_dodge(width=1.2),alpha = 0.7,scale="width",adjust=0.9,trim=FALSE)+
geom_boxplot(width=0.4,outlier.colour = "transparent")+
geom_point(aes(fill = behaviour), size = 5, shape = 21, position = position_jitterdodge()) +
theme_classic()+
scale_fill_manual(values = c("E" = '#3797a4', "R"= '#96bb7c',"S"= '#944e6c'))+
theme(text = element_text(size = 18),
axis.title.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
legend.position = "right",
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title = element_blank()
)+
ylim(0, 14)
shannonviolinfill+geom_signif(data = as.data.frame(d), tip_length = 0.01, aes(xmin=group1, xmax=group2, annotations=labels,y_position=c(12.4,13,13.6)),manual=TRUE)
我得到的错误是
ERROR while rich displaying an object: Error: Aesthetics must be either length 1 or the same as the data (3): fill
理想情况下,它应该如下所示:
如果有人知道黑客或解决方法,我将不胜感激,因为我整天都在绕着下水道转!
解决方案
问题是你做fill = behaviour
了一个全球性的AES。behaviour
但是,您的 dataset中没有命名列d
。作为一种解决方法,通过从 and 中删除它来使其成为本地,ggplot()
而不是aes(fill = behaviour)
在两者中使用geom_violin
and geom_boxcplot
:
set.seed(20)
col1<-c(rep('E', each = 8))
col2<-c(rep('R', each = 8))
col3<-c(rep('S', each = 8))
behaviour<-c(col1,col2,col3)
value <- runif(length(behaviour), min=2, max=8)
shannon <- c(rep('Shannon', each = length(behaviour)))
test.data <- data.frame(behaviour, value, shannon)
library(ggpubr)
#> Loading required package: ggplot2
library(magrittr)
library(ggplot2)
d <- compare_means(value~behaviour, data = test.data,method = 't.test')
d %<>% mutate(y_pos = c(5,5.5,6),labels = ifelse(p<0.17,p.format,p.signif))
shannonviolin<-ggplot(test.data, aes(x = behaviour, y = value)) +
labs(y= "Shannon",color = " ",size=8)+
geom_violin(aes(fill = behaviour), position=position_dodge(width=1.2),alpha = 0.7,scale="width",adjust=0.9,trim=FALSE)+
geom_boxplot(aes(fill = behaviour), width=0.4,outlier.colour = "transparent")+
geom_point(aes(fill = behaviour), size = 5, shape = 21, position = position_jitterdodge()) +
theme_classic()+
scale_fill_manual(values = c("E" = '#3797a4', "R"= '#96bb7c',"S"= '#944e6c'))+
theme(text = element_text(size = 18),
axis.title.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
legend.position = "right",
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title = element_blank()
)+
ylim(0, 14)
shannonviolin+geom_signif(data = as.data.frame(d), tip_length = 0.01, aes(xmin=group1, xmax=group2, annotations=labels,y_position=c(12.4,13,13.6)),manual=TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
#> Warning: Removed 141 rows containing missing values (geom_violin).
由reprex 包于 2021-02-04 创建(v1.0.0)
推荐阅读
- mysql - 如何让返回等待 MySQL 连接结束?节点.js
- c++ - 为什么旋转和方向之间有90度的差异?
- c++ - 如果字符串尚未打印,则打印字符串
- python - 将数据加载到 X_train 和 Y_train
- c# - 使用 C# 访问 xml 标签并更新内容
- python - Tensorflow 最小化功能无法正常工作
- xcode - App Store 拒绝 - 指南 5.1.2 - 法律 - 隐私 - 数据使用和共享
- python - Python嵌入的“threading.local”意外行为
- python - 为什么 pytest 输出 ANSI 转义字符?
- arrays - 绘制具有单独分量数组的二维向量