如何使用ggsignif软件包将p值添加到堆积条形图中?

时间:2018-11-09 13:17:06

标签: r ggplot2

我正在尝试使用ggsignif软件包将大量p值添加到多面堆积条形图中,但收到一条错误消息

  

check_factor(f)中的错误:找不到对象“等级”

任何有关如何解决此问题的建议,将不胜感激!下面是重现我的问题的数据和代码:

GArray

1 个答案:

答案 0 :(得分:0)

您可以尝试

p <- ggplot(df, aes(Diet, Percent, fill = Rank)) + 
  geom_col() +
  facet_wrap(~ Variable) +
  geom_signif(annotations = 0.03, y_position = 105 ,xmin="IM", xmax="REF")
p

enter image description here

在另一个方面添加注释,您必须使用此处How to annotate different values for each facet (bar plot) on R?的代码对绘图后面的数据进行硬编码

p <- ggplot_build(p)
p$data[[2]] <- rbind(p$data[[2]],p$data[[2]]) # rbind a second annotation a three rows
p$data[[2]]$PANEL[4:6] <- 2 # panel 2
p$data[[2]]$annotation[4:6] <- "your text" 
plot(ggplot_gtable(p))

enter image description here

我使用了ggsignif_0.4.0ggplot2_3.0.0 数据

df <- data.frame(Diet = rep(c("REF", "IM"), each = 8),
                 Variable = c("hpv", "hpv", "hpv", "hpv", "smc", "smc", "lpc", "lpc",
                              "hpv", "hpv", "hpv", "smc", "smc", "smc", "lpc", "lpc"),
                 Rank = c("Mild", "Moderate", "Marked", "Severe", "Normal", "Mild", "Normal", "Mild",
                          "Mild", "Moderate", "Marked", "Normal", "Mild", "Moderate", "Normal", "Mild"),
                 Percent = c(5.56, 38.9, 44.4, 11.1, 38.9, 61.1, 77.8, 22.2, 
                             16.7, 66.7, 16.7, 11.1, 72.2, 16.7, 50, 50)
                 )