无法将 abline 添加到 ggplot2 条形图
Trouble adding abline to ggplot2 bar graph
我正在尝试使用 geom_vline
向我在 ggplot2 中生成的图表添加一条垂直线,但它没有显示出来。我想要的只是一条直线 运行s 垂直穿过 x=0。这是数据和代码 运行:
#Example data for Stack Overflow
sem <- data.frame(brand = c('com','com','com','sus','sus','sus','tol','tol','tol'),
rate = c(1,
2,
3,
1,
2,
3,
1,
2,
3),
sem = c(-100.652190547299,
-20.9635462903477,
-92.887143790098,
-32.5321197096671,
-10.8046113120258,
-103.882668200279,
39.1133320990038,
50.641868900031,
27.3390542856909))
percent_diff <- data.frame(brand = c('com','com','com','sus','sus','sus','tol','tol','tol'),
rate = c(1,
2,
3,
1,
2,
3,
1,
2,
3),
percent_diff = c(-16.8547043500825,
-123.651964249353,
-70.2307389653605,
-316.119165728843,
-290.448196586088,
-276.236250440114,
23.6027946419299,
35.415138795611,
52.9344042281165))
#Left-join the data into one data frame
library(dplyr)
df <- left_join(percent_diff, sem)
#Generate the graph
library(ggplot2)
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_vline(xintercept = 0)+
scale_fill_discrete(name="Rate", labels=c("1X", "2X", "3X"))+
xlab("Brand")+ylab("Percent Difference (Compared to nontreated)")+
geom_errorbar(aes(ymin= percent_diff, ymax=percent_diff+sem), width=0.2, position = position_dodge(0.6))+
ggtitle("Brand Comparison")+
scale_fill_brewer(palette='Greys', name="Rate", labels=c("1X", "2X", "3X"))+
theme(plot.title = element_text(hjust=0.5))+
coord_flip()+
geom_vline(aes(xintercept=0, col = 'red', size = 3))
结果是这样的:
为什么会出现此输出而不是单行 运行ning 垂直?我尝试加载 library(scales)
包,但没有得到我希望的结果。
coord_flip()
可能会造成混淆。在应用 coord_flip
之前,您需要根据 原始 x 和 y 来定义绘图。在这种情况下,这意味着使用 geom_hline
代替:
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_hline(yintercept=0, col = 'red', size = 3)
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_hline(yintercept=0, col = 'red', size = 3) +
coord_flip()
请注意,您可以使用 geom_col()
而不是 geom_bar(stat = 'identity')
。
只需将 geom_vline
更改为 geom_hline
。问题是一切都是用原始坐标和轴绘制的。因此,你想要的线实际上是一条水平线,后来像所有带 coord_flip
的情节一样翻转。这是我对您的代码所做的唯一修改:
#I also passed col and size outside aes
geom_hline(aes(yintercept=0),col = 'red', size = 1)
我正在尝试使用 geom_vline
向我在 ggplot2 中生成的图表添加一条垂直线,但它没有显示出来。我想要的只是一条直线 运行s 垂直穿过 x=0。这是数据和代码 运行:
#Example data for Stack Overflow
sem <- data.frame(brand = c('com','com','com','sus','sus','sus','tol','tol','tol'),
rate = c(1,
2,
3,
1,
2,
3,
1,
2,
3),
sem = c(-100.652190547299,
-20.9635462903477,
-92.887143790098,
-32.5321197096671,
-10.8046113120258,
-103.882668200279,
39.1133320990038,
50.641868900031,
27.3390542856909))
percent_diff <- data.frame(brand = c('com','com','com','sus','sus','sus','tol','tol','tol'),
rate = c(1,
2,
3,
1,
2,
3,
1,
2,
3),
percent_diff = c(-16.8547043500825,
-123.651964249353,
-70.2307389653605,
-316.119165728843,
-290.448196586088,
-276.236250440114,
23.6027946419299,
35.415138795611,
52.9344042281165))
#Left-join the data into one data frame
library(dplyr)
df <- left_join(percent_diff, sem)
#Generate the graph
library(ggplot2)
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_vline(xintercept = 0)+
scale_fill_discrete(name="Rate", labels=c("1X", "2X", "3X"))+
xlab("Brand")+ylab("Percent Difference (Compared to nontreated)")+
geom_errorbar(aes(ymin= percent_diff, ymax=percent_diff+sem), width=0.2, position = position_dodge(0.6))+
ggtitle("Brand Comparison")+
scale_fill_brewer(palette='Greys', name="Rate", labels=c("1X", "2X", "3X"))+
theme(plot.title = element_text(hjust=0.5))+
coord_flip()+
geom_vline(aes(xintercept=0, col = 'red', size = 3))
结果是这样的:
为什么会出现此输出而不是单行 运行ning 垂直?我尝试加载 library(scales)
包,但没有得到我希望的结果。
coord_flip()
可能会造成混淆。在应用 coord_flip
之前,您需要根据 原始 x 和 y 来定义绘图。在这种情况下,这意味着使用 geom_hline
代替:
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_hline(yintercept=0, col = 'red', size = 3)
ggplot(df, aes(x=brand, y=percent_diff, fill=factor(rate)))+
geom_bar(stat="identity",width=0.6, position="dodge", col="black")+
geom_hline(yintercept=0, col = 'red', size = 3) +
coord_flip()
请注意,您可以使用 geom_col()
而不是 geom_bar(stat = 'identity')
。
只需将 geom_vline
更改为 geom_hline
。问题是一切都是用原始坐标和轴绘制的。因此,你想要的线实际上是一条水平线,后来像所有带 coord_flip
的情节一样翻转。这是我对您的代码所做的唯一修改:
#I also passed col and size outside aes
geom_hline(aes(yintercept=0),col = 'red', size = 1)