尝试在ggplot中指示条形图的统计显着性差异时,出现注释错误

时间:2020-01-16 22:04:11

标签: r ggplot2

我的代码如下:

p <- ggplot(data = df, aes(x = Name, y = prop, fill = Name)) +
  geom_bar(stat = "identity") +
  labs(x = "", y = "EQTL / gene") +
  scale_fill_brewer(palette="Greens",name = "Number of cis EQTL") + 
  theme_classic()+
  theme(panel.grid.major.x = element_line(size = 0.1, color = "grey"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank()
  )

p+geom_path(x=c(1,1,2,2),y=c(0.85,0.86,0.86,0.85))+
  annotate("text",x=1.5,y=1.2,label="p = 2e-16")

我想做的就是在两条横条的中点之间放置一条水平线,上面写成:p = 2e-16

但是当我运行代码时,出现此错误:

Error in annotate("text", x = 1.5, y = 1.2, label = "p = 2e-16") : 
  unused arguments (x = 1.5, y = 1.2, label = "p = 2e-16")

enter image description here

df <- readr::read_table(' prop                          Name
1 0.85            All_Genes
2 1.00 Glucose_Response_Genes103')

解决方案:

p <- ggplot(data = df, aes(x = Name, y = prop, fill = Name)) +
  geom_bar(stat = "identity") +
  labs(x = "", y = "Proportion of eGenes") +
  scale_fill_brewer(palette="Greens",name = "Number of cis EQTL", labels = c("3124345", "26846")) + 
  theme_minimal() +
  theme(legend.position = "right",
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.line = element_line(),
        axis.ticks = element_line())

p + ggplot2::annotate("text", x = 1.5, y = 1.2, label = "p < 2e-16", size = 3.5) +  
  ggplot2::annotate("rect", xmin = 1, xmax = 2, ymin = 1.1, ymax =1.1, alpha=0.3,colour = "black")

@Axeman指出问题在于:ggplot2 :: annotate

我还是想知道如何编辑此代码并获得更小,更优雅的条形图?

1 个答案:

答案 0 :(得分:1)

问题不是注释调用。这是您的geom_path调用,需要一个数据框。我可能会使用annotate(geom = 'segment')而不是geom_path进行注释,但是我们开始:

library(ggplot2)
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_bar(stat = "identity") +
  geom_path(data = data.frame(x=c(1,1,2,2), y=c(0.85,0.86,0.86,0.85)), aes(x,y))+
  annotate("text",x=1.5,y=1.2,label="p = 2e-16") 

reprex package(v0.3.0)于2020-01-16创建