0
votes

I am trying to use the geom_vline to add a vertical line to my graph generated in ggplot2, but it's not showing up. All I want is a single line that runs vertically through x=0. Here is the data and the code run:

#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))

The result is this:

enter image description here

Why does this output occur instead of a single line running vertically? I tried loading the library(scales) package, but I did not get the results I was hoping for.

2
since you used coord_flip() the axes are inverted, you need to use geom_hline() if you want a vertical lineEJJ

2 Answers

3
votes

Just change geom_vline to geom_hline. The problem is that everything is plotted with the original coordinates and axes. Therefore, the line you want is in fact a horizontal line that later becomes flipped like all the plot with coord_flip. Here is the only modification I made to your code:

#I also passed col and size outside aes  
geom_hline(aes(yintercept=0),col = 'red', size = 1)

Plot with red line

1
votes

coord_flip() can be confusing. You need to define your plot in terms of the original x and y, before applying coord_flip. In this case, that means using geom_hline instead:

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)

enter image description here

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()

enter image description here

Note that you can use geom_col() instead of geom_bar(stat = 'identity').