将手动 p 值添加到 ggplot2 中的分组条形图

Adding manual p-values to grouped bargraph in ggplot2

我正在尝试将手动 p 值添加到 ggplot2 上的分组条形图中。我尝试了之前帖子中建议的 geom_sigf 函数(使用手动坐标),但我的图表上没有输出。我想为图中的三个比较添加“<0.001”。

data <- data.frame(group = c("A", "B","A", "B","A", "B"),
               variable = c("X", "X", "Y", "Y", "Z", "Z"),
               value = c(10, 40, 1, 15, 10, 25))

ggplot(data = data, aes(fill=group, x=variable, y=value)) +
 geom_bar(position="dodge", stat = "identity") +
 geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.2, size = 5) +
 labs(x = "") +
 labs(y="Proportion (%)") +
 theme(legend.title = element_blank(), legend.position = "bottom") +
 theme(axis.text.x = element_text(size=12, color = "black")) +
 theme(axis.text.y = element_text(size = 12)) +
 theme(legend.text = element_text(size = 12)) +
 theme(axis.title.y = element_text(size = 12)) +
 scale_y_continuous(limits = c(0,60)) 

`

非常感谢!

我通常通过使用我的 p 值和来自不同数据框的标签添加一个 geom_text 层来解决这个问题。您必须将填充美学移动到使用它的几何体,因为它不存在于 annot 数据框中。这样做的好处是您可以开发管道来计算 p 值并以编程方式插入它们。通过使用 vjust 将 ypos 设置为 Inf,文本应该始终靠近图表的顶部。这应该有效:

annot <- 
  tibble(
    variable = c("X", "Y", "Z"), 
    label = rep("< 0.001", 3), 
    ypos = Inf, 
    vjustvar = 2
  )

ggplot(data = data, aes(x=variable, y=value)) +
  geom_bar(aes(fill = group), position="dodge", stat = "identity") +
  geom_text(aes(fill = group, label=value), position=position_dodge(width=0.9), vjust=-0.2, size = 5) +
  geom_text(
    data = annot, 
    aes(label = label, y = ypos, vjust = vjustvar)
  ) +
  labs(x = "") +
  labs(y="Proportion (%)") +
  theme(legend.title = element_blank(), legend.position = "bottom") +
  theme(axis.text.x = element_text(size=12, color = "black")) +
  theme(axis.text.y = element_text(size = 12)) +
  theme(legend.text = element_text(size = 12)) +
  theme(axis.title.y = element_text(size = 12)) +
  scale_y_continuous(limits = c(0,60)) 

您可以使用注释:

data <- data.frame(group = c("A", "B","A", "B","A", "B"),
                   variable = c("X", "X", "Y", "Y", "Z", "Z"),
                   value = c(10, 40, 1, 15, 10, 25))

ggplot(data = data, aes(fill=group, x=variable, y=value)) +
  geom_bar(position="dodge", stat = "identity") +
  geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.2, size = 5) +
  labs(x = "") +
  labs(y="Proportion (%)") +
  theme(legend.title = element_blank(), legend.position = "bottom") +
  theme(axis.text.x = element_text(size=12, color = "black")) +
  theme(axis.text.y = element_text(size = 12)) +
  theme(legend.text = element_text(size = 12)) +
  theme(axis.title.y = element_text(size = 12)) +
  scale_y_continuous(limits = c(0,60))  +
  annotate(geom="text", x=3, y=50, label="<0.001",
           color="black") +
  annotate(geom="text", x=2, y=50, label="<0.001",
           color="black") + 
  annotate(geom="text", x=1, y=50, label="<0.001",
           color="black")
data <- data.frame(group = c("A", "B","A", "B","A", "B"),
variable = c("X", "X", "Y", "Y", "Z", "Z"),
value = c(10, 40, 1, 15, 10, 25))

ggplot(data = data, aes(fill=group, x=variable, y=value)) +
geom_bar(position="dodge", stat = "identity") +
geom_text(aes(label=value), 
position=position_dodge(width=0.9), vjust=-0.2, size = 5) +
labs(x = "") +
labs(y="Proportion (%)") +
theme(legend.title = element_blank(), legend.position = 
"bottom") +
theme(axis.text.x = element_text(size=12, color = 
"black")) +
theme(axis.text.y = element_text(size = 12)) +
theme(legend.text = element_text(size = 12)) +
theme(axis.title.y = element_text(size = 12)) +
scale_y_continuous(limits = c(0,60)) +
annotate("rect", xmin = 0.75, xmax = 1.25, ymin = 50, ymax 
=50, alpha = 1, color = "black") +
annotate("rect", xmin = 0.75, xmax = 0.75, ymin = 49, ymax 
=50.09, alpha=1, colour = "black") +
annotate("rect", xmin = 1.25, xmax = 1.25, ymin = 49, ymax 
=50.09, alpha=1, colour = "black")+       
annotate("rect", xmin = 1.75, xmax = 2.25, ymin = 25, ymax 
=25, alpha = 1, color = "black") +
annotate("rect", xmin = 1.75, xmax = 1.75, ymin = 24, ymax 
=25.09, alpha=1, colour = "black") +
annotate("rect", xmin = 2.25, xmax = 2.25, ymin = 24, ymax 
=25.09, alpha=1, colour = "black") +
annotate("rect", xmin = 2.75, xmax = 3.25, ymin = 36, ymax 
=36, alpha = 1, color = "black") +
annotate("rect", xmin = 2.75, xmax = 2.75, ymin = 35, ymax 
=36.09, alpha=1, colour = "black") +
annotate("rect", xmin = 3.25, xmax = 3.25, ymin = 35, ymax 
=36.09, alpha=1, colour = "black") +
annotate(geom = "text", x = 1.0, y = 52, label = "p 
<0.01", 
color = "black") +
annotate(geom = "text", x = 2.0, y = 27, label = "p 
<0.01", 
color = "black") +
annotate(geom = "text", x = 3.0, y = 38, label = "p 
<0.01", 
color = "black") 

一点也不优雅,但能够注释水平和垂直线以创建括号并添加 p 值。下次一定要试试 ggbracket。