【问题标题】:ggplot how to set y-position with geom_signif when the y-axis is logarithmic当y轴为对数时,ggplot如何使用geom_signif设置y位置
【发布时间】:2021-02-09 10:49:37
【问题描述】:

我无法正确放置此图的标签。 “正确”是指不在顶部卡住或重叠,而是沿 y 轴向下移动到适当的位置。请参阅下面的可重现示例和图表。当前的标记方法意味着忽略 y 轴位置。我该如何解决这个问题?

library(magrittr) 
library(dplyr) 
library(ggpubr)

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=0, max=0.0006)
species <- c(rep('B_theta', each = length(behaviour)))
test.data <- data.frame(behaviour, value, species)

d <- compare_means(value~behaviour, data = test.data,method = 't.test')
d %<>% mutate(y_pos = c(1.004,1.156763e-06,5.882128e-04),labels = ifelse(p<0.15,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  1.004000e+00    ns
value   E   S   0.1559682   0.47    0.16    ns          T-test  1.156763e-06    0.16
value   R   S   0.3794209   0.76    0.38    ns          T-test  5.882128e-04    ns


ggplot(data=subset(test.data, !is.na(value)), aes(x=behaviour,y=value)) + 
 geom_boxplot(aes(fill = behaviour), width=0.4,outlier.colour = "transparent")+
  geom_point(aes(fill = behaviour), size = 5, shape = 21, position = position_jitterdodge()) +
  scale_fill_manual(values = c("E" = '#3797a4', "R"= '#96bb7c',"S"= '#944e6c'))+
theme_classic()+
scale_y_log10() + annotation_logticks(sides = "l")+
geom_signif(data = as.data.frame(d), textsize=6, tip_length = 0.01, 
            aes(xmin=group1, xmax=group2, annotations=labels,y_position=y_pos),manual=TRUE)

Warning message:
"Ignoring unknown aesthetics: xmin, xmax, annotations, y_position"

这是我目前得到的:

理想的输出:

【问题讨论】:

  • geom_siginif 函数从何而来?您能否将必要的软件包添加到您的 MRE 中?
  • 嗨,我已将库添加到编辑中。谢谢

标签: r ggplot2


【解决方案1】:

我在重现您的示例时遇到了一些问题。你确定你提供的代码给了你这个数字吗?我真的很难理解 0.16 的 p 值如何小于 0.15 ??? 另外,我不熟悉geom_signif 功能,我所拥有的所有信息都是我在它的帮助中找到的(?geom_signif) 无论如何,通过一些小的改动,我能够创建您想要的情节:

library(magrittr) 
library(dplyr) 
library(ggpubr)

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=0, max=0.0006)
species <- c(rep('B_theta', each = length(behaviour)))
test.data <- data.frame(behaviour, value, species)

d <- compare_means(value~behaviour, data = test.data,method = 't.test')

# Here you manually specify the y-position of the significance bars
# The plot you got, was exactly what you specified here.
# But since we are using log10 scale for y Axis we should probably indicate
# The exponent i.e. -1, 0, 1 (as seen in your desired output plot)
# ALSO: I could not reproduce the significant result with your value for alpha of 0.15 -> had to use 0.25
#
# P.S. I will never use %<>% in my code. Ever! ;) (In my work, %<>% is an excellent footgun - it might save halve a second of typing,
# but you can only hope that nobody has ever to read the code again)
#
d <- d %>%  mutate(y_pos =  c(-1, 0, 1),labels = ifelse(p < 0.25, p.format, p.signif))

ggplot(data=subset(test.data, !is.na(value)), aes(x=behaviour,y=value)) + 
  geom_boxplot(aes(fill = behaviour), width=0.4,outlier.colour = "transparent")+
  geom_point(aes(fill = behaviour), size = 5, shape = 21, position = position_jitterdodge()) +
  scale_fill_manual(values = c("E" = '#3797a4', "R"= '#96bb7c',"S"= '#944e6c'))+
  theme_classic()+
  scale_y_log10() + annotation_logticks(sides = "l") +
  
  # The syntax you used did not correspond to the example I found in
  # ?geom_signif
  # If we just follow the example everything works and the warnings disappear
  #
  geom_signif(textsize=6, tip_length = 0.01, 
              xmin=d$group1, xmax=d$group2, annotations=d$labels, y_position=d$y_pos)

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2022-12-19
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多