【问题标题】:stat_compare_means conditional p-value labels causes ggplot fill to failstat_compare_means 条件 p 值标签导致 ggplot 填充失败
【发布时间】:2021-02-04 16:25:11
【问题描述】:

按照this post 的示例,我的目标是仅在 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

理想情况下应该是这样的:

如果有人知道黑客或解决方法,我将不胜感激,因为我整天都在绕着下水道转!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    问题是您将fill = behaviour 设为全局aes。但是,您的数据集 d 中没有名为 behaviour 的列。作为一种解决方法,通过将其从 ggplot() 中删除来使其成为本地,而不是在 geom_violingeom_boxcplot 中使用 aes(fill = behaviour)

    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 package (v1.0.0) 于 2021-02-04 创建

    【讨论】:

    • 非常感谢!
    • 对不起,还有一个问题。你用什么IDE来运行R?你的情节比我的分辨率高得多。我正在使用 jupyter 笔记本。
    • 我正在使用 RStudio。目前在 Mac 上。但是当我在我的 Windows 机器上时,我的情节在 RStudio 中看起来也不太好。 (;
    猜你喜欢
    • 2021-09-30
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多