r - 如何使用 ggsignif 包将 p 值添加到堆积条形图?
问题描述
我试图使用 ggsignif 包将重要的 p 值添加到多面堆积条形图中,但收到一条错误消息
check_factor(f) 中的错误:找不到对象“等级”
非常感谢任何有关如何解决此问题的建议!以下是重现我的问题的数据和代码:
library(tidyverse)
library(cowplot)
#>
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#>
#> ggsave
library(ggsignif)
# Make a dataframe for plotting stacked bar plot
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)
)
# Specify the desired orders of factors and convert "Rank" to an ordered factor
df$Diet <- factor(df$Diet, levels = c("REF", "IM"))
df$Variable <- factor(df$Variable, levels = c("hpv", "smc", "lpc"))
df$Rank <- ordered(df$Rank, levels = c("Normal", "Mild", "Moderate", "Marked", "Severe")) # Rank as ordered factor
# Define color scheme
my_col = c(Normal = "royalblue2", Mild = "peachpuff1", Moderate = "tan1", Marked = "tomato", Severe = "red3")
# Make stacked barplot
p <- ggplot(df, aes(Diet, Percent, fill = forcats::fct_rev(Rank))) + # forcats::fct_rev() reverses stacked bars
geom_bar(stat = "identity") +
facet_wrap(~ Variable, nrow = 1) +
scale_fill_manual(values = my_col) +
scale_y_continuous(limits = c(0, 105), breaks = 0:5*20, expand = expand_scale(mult = c(0, 0.05))) +
labs(title = "Stacked bar plot", y = "%") +
guides(fill = guide_legend(title = "Rank")) +
theme_cowplot()
# Make a datafraome for p value annotation
anno <- data.frame(Variable = "hpv",
p = 0.03,
start = "REF",
end = "IM",
y = 102)
# Add p value to the plot
p + geom_signif(data = anno,
aes(xmin = start,
xmax = end,
annotations = p,
y_position = y),
textsize = 4,
tip_length = 0,
manual = TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
#> Error in check_factor(f): object 'Rank' not found
解决方案
你可以试试
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
在另一个方面添加注释,您必须使用此处的代码对绘图背后的数据进行硬编码如何注释 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))
我用过ggsignif_0.4.0
&ggplot2_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)
)
推荐阅读
- vue.js - 是否可以在没有 vue-cli 的情况下构建 vue 应用程序?
- sql - sql - 列中的最后一个值和时钟值
- java - 在抽象类和子方法之间使用 Spring 事务代理
- php - 语法错误,包括 Laravel 在内的 javascript 上出现意外的“公共”(T_PUBLIC)
- javascript - 如何加密电子邮件 ID 并将其解密,但字符串长度应为 30-40 。?
- javascript - 如何使用javascript发出异步请求以同步请求
- python - 能量系统的 Python GEKKO MINLP 优化:如何构建二维数组的中间体
- kubernetes - 在 configmap 中参数化 grafana.ini
- mysql - 您可以在一个表中定义一个外键,从该表中派生出具有主键的表吗?
- reactjs - 在 Redux 中存储大数据好吗?