在 ggplot 条形图中的每个 x 轴内的多个条形图之间添加显着性星号
Add significance asterisks to graph between multiple bars within each x-axis in ggplot bar graph
我试图在 ggplot
条形图中指出哪些治疗与我的每个 x 轴标签中的控制显着不同。
我将分类学作为 x 轴,每个柱内有四个不同的条,表示实验处理和这些分类群中每一个作为 y 轴的处理的生长。
我附上了使用提供的代码制作的示例图的屏幕截图(代码中消除了误差条)以及一些虚拟数据,其中“Y”表示控制的重要性(如果是的话,请随意制作这些数字更容易)。
我试过使用 geom_signif
,但我似乎无法在同一个 x 轴列中添加不止一个比较,这表明仅列本身之间存在差异。
我也试过为每个星号绘制一个 geom_point
值,但无法将星号与其各自的处理栏对齐。
如有任何帮助,我们将不胜感激。
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment)) +
geom_bar(stat="identity", size=0.5, color="black", position=position_dodge())+
scale_fill_manual(values=colorsvalue, name = expression(paste(Treatment)), labels=leglabs, position="top")+
ylab(expression( Growth~ (day^{-1})))+
theme_classic()+
theme (axis.title.x=element_blank()) +
scale_x_discrete (labels = xlabs)+
theme (axis.text.x = element_text(angle=90, hjust=1, size=10))+
theme(text = element_text(size=20))
图表
示例数据
一种方法是绘制包含 'Y' 的因子作为星号。您可以通过首先将每个值的位置计算为 new.x 并用新的 y 值进行偏移来做到这一点。您可以根据需要调整 new.y 值。因为每个图上有四个图,所以条形中心的末端是 (0.25 + 0.5)/2,中间条形是 (0.25 + 0)/2。负数位于条形图的左侧。作为参考,我在
上使用了第二个示例
library(dplyr)
Experiment <- Experiment %>% mutate(new.x = ifelse(Treatment == "0.25", -0.375,
ifelse(Treatment == "Control", -0.125,
ifelse(Treatment == "D. magna", 0.125, 0.375))) +
as.numeric(as.factor(Taxa)))
Experiment$new.y <- Experiment$Growth +1
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment)) +
geom_col(size=0.5, color="black", position=position_dodge())+
geom_point(data=subset(Experiment, Star == 'Y'), aes(x=new.x, y=new.y), shape=8, show.legend=FALSE) +
#scale_fill_manual(values=colorsvalue, name = expression(paste(Treatment)), labels=leglabs, position="top")+
ylab(expression( Growth~ (day^{-1})))+
theme_classic()+
theme (axis.title.x=element_blank()) +
#scale_x_discrete (labels = xlabs)+
theme (axis.text.x = element_text(angle=90, hjust=1, size=10))+
theme(text = element_text(size=20))
隐藏在中的是将position_dodge
的宽度设置为0.9以将点与条对齐的技巧。然后,您可以将形状添加到原始 aes
,但将一个形状设置为 NA
,将一个形状设置为 8
(星形)。您也可以在 geom_point
.
的 aes
中加 1
ggplot(Experiment, aes(x = Taxa, y = Growth, fill = Treatment, shape = star)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_point(aes(y = Growth + 1),
position = position_dodge(0.9),
show.legend = FALSE) +
scale_shape_manual(values = c(NA, 8))
数据
structure(list(Taxa = c("Cyano", "Cyano", "Cyano", "Cyano", "Dolicho",
"Dolicho", "Dolicho", "Dolicho", "Gompho", "Gompho", "Gompho",
"Gompho", "Nosto", "Nosto", "Nosto", "Nosto"), Treatment = c("Control",
"D. pulex", "D. magna", "0.25", "Control", "D. pulex", "D. magna",
"0.25", "Control", "D. pulex", "D. magna", "0.25", "Control",
"D. pulex", "D. magna", "0.25"), Growth = 2:17, star = c("",
"Y", "Y", "", "", "Y", "Y", "Y", "", "", "", "Y", "", "", "",
"Y")), class = "data.frame", row.names = c(NA, -16L))
编辑
负数示例,使用 ifelse
语句从星号的 y 位置加 1 或减去。
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment, shape = star)) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_point(aes(y = ifelse(Growth > 0, Growth + 1, Growth - 1)),
position=position_dodge(0.9),
show.legend=FALSE) +
scale_shape_manual(values = c(NA, 8))
有负数的数据
structure(list(Taxa = c("Cyano", "Cyano", "Cyano", "Cyano", "Dolicho",
"Dolicho", "Dolicho", "Dolicho", "Gompho", "Gompho", "Gompho",
"Gompho", "Nosto", "Nosto", "Nosto", "Nosto"), Treatment = c("Control",
"D. pulex", "D. magna", "0.25", "Control", "D. pulex", "D. magna",
"0.25", "Control", "D. pulex", "D. magna", "0.25", "Control",
"D. pulex", "D. magna", "0.25"), Growth = -10:5, star = c("",
"Y", "Y", "", "", "Y", "Y", "Y", "", "", "", "Y", "", "", "",
"Y")), class = "data.frame", row.names = c(NA, -16L))
我试图在 ggplot
条形图中指出哪些治疗与我的每个 x 轴标签中的控制显着不同。
我将分类学作为 x 轴,每个柱内有四个不同的条,表示实验处理和这些分类群中每一个作为 y 轴的处理的生长。
我附上了使用提供的代码制作的示例图的屏幕截图(代码中消除了误差条)以及一些虚拟数据,其中“Y”表示控制的重要性(如果是的话,请随意制作这些数字更容易)。
我试过使用 geom_signif
,但我似乎无法在同一个 x 轴列中添加不止一个比较,这表明仅列本身之间存在差异。
我也试过为每个星号绘制一个 geom_point
值,但无法将星号与其各自的处理栏对齐。
如有任何帮助,我们将不胜感激。
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment)) +
geom_bar(stat="identity", size=0.5, color="black", position=position_dodge())+
scale_fill_manual(values=colorsvalue, name = expression(paste(Treatment)), labels=leglabs, position="top")+
ylab(expression( Growth~ (day^{-1})))+
theme_classic()+
theme (axis.title.x=element_blank()) +
scale_x_discrete (labels = xlabs)+
theme (axis.text.x = element_text(angle=90, hjust=1, size=10))+
theme(text = element_text(size=20))
图表
示例数据
一种方法是绘制包含 'Y' 的因子作为星号。您可以通过首先将每个值的位置计算为 new.x 并用新的 y 值进行偏移来做到这一点。您可以根据需要调整 new.y 值。因为每个图上有四个图,所以条形中心的末端是 (0.25 + 0.5)/2,中间条形是 (0.25 + 0)/2。负数位于条形图的左侧。作为参考,我在
library(dplyr)
Experiment <- Experiment %>% mutate(new.x = ifelse(Treatment == "0.25", -0.375,
ifelse(Treatment == "Control", -0.125,
ifelse(Treatment == "D. magna", 0.125, 0.375))) +
as.numeric(as.factor(Taxa)))
Experiment$new.y <- Experiment$Growth +1
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment)) +
geom_col(size=0.5, color="black", position=position_dodge())+
geom_point(data=subset(Experiment, Star == 'Y'), aes(x=new.x, y=new.y), shape=8, show.legend=FALSE) +
#scale_fill_manual(values=colorsvalue, name = expression(paste(Treatment)), labels=leglabs, position="top")+
ylab(expression( Growth~ (day^{-1})))+
theme_classic()+
theme (axis.title.x=element_blank()) +
#scale_x_discrete (labels = xlabs)+
theme (axis.text.x = element_text(angle=90, hjust=1, size=10))+
theme(text = element_text(size=20))
隐藏在position_dodge
的宽度设置为0.9以将点与条对齐的技巧。然后,您可以将形状添加到原始 aes
,但将一个形状设置为 NA
,将一个形状设置为 8
(星形)。您也可以在 geom_point
.
aes
中加 1
ggplot(Experiment, aes(x = Taxa, y = Growth, fill = Treatment, shape = star)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_point(aes(y = Growth + 1),
position = position_dodge(0.9),
show.legend = FALSE) +
scale_shape_manual(values = c(NA, 8))
数据
structure(list(Taxa = c("Cyano", "Cyano", "Cyano", "Cyano", "Dolicho",
"Dolicho", "Dolicho", "Dolicho", "Gompho", "Gompho", "Gompho",
"Gompho", "Nosto", "Nosto", "Nosto", "Nosto"), Treatment = c("Control",
"D. pulex", "D. magna", "0.25", "Control", "D. pulex", "D. magna",
"0.25", "Control", "D. pulex", "D. magna", "0.25", "Control",
"D. pulex", "D. magna", "0.25"), Growth = 2:17, star = c("",
"Y", "Y", "", "", "Y", "Y", "Y", "", "", "", "Y", "", "", "",
"Y")), class = "data.frame", row.names = c(NA, -16L))
编辑
负数示例,使用 ifelse
语句从星号的 y 位置加 1 或减去。
ggplot(Experiment, aes(x=Taxa, y=Growth, fill=Treatment, shape = star)) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_point(aes(y = ifelse(Growth > 0, Growth + 1, Growth - 1)),
position=position_dodge(0.9),
show.legend=FALSE) +
scale_shape_manual(values = c(NA, 8))
有负数的数据
structure(list(Taxa = c("Cyano", "Cyano", "Cyano", "Cyano", "Dolicho",
"Dolicho", "Dolicho", "Dolicho", "Gompho", "Gompho", "Gompho",
"Gompho", "Nosto", "Nosto", "Nosto", "Nosto"), Treatment = c("Control",
"D. pulex", "D. magna", "0.25", "Control", "D. pulex", "D. magna",
"0.25", "Control", "D. pulex", "D. magna", "0.25", "Control",
"D. pulex", "D. magna", "0.25"), Growth = -10:5, star = c("",
"Y", "Y", "", "", "Y", "Y", "Y", "", "", "", "Y", "", "", "",
"Y")), class = "data.frame", row.names = c(NA, -16L))