I am trying to use annotate_custom
to create lines outside of my plot to divide my axis into sections. I am aware of this post that asks a similar question, but for some reason, using a negative value as the min value for the line does not extend the lines outside of the plot.
Sample code:
library(ggplot2)
library(grid)
data("iris")
ggplot(iris, aes(x=Species, y=Petal.Width)) +
geom_bar(stat='identity')+coord_flip()+
annotation_custom(grob = linesGrob(), xmin = 2.5, xmax = 2.5, ymin = -90, ymax = 0)+
annotation_custom(grob = linesGrob(), xmin = 1.5, xmax = 1.5, ymin = -90, ymax = 0)
Be default, the setting is to not allow any graphical elements to clip outside the plot area. You can turn off clipping via any of the coord_*
functions (ex. coord_cartesian()
, coord_fixed()
...), so in your case, use coord_flip(clip="off")
to allow grobs to extend anywhere in your plot:
ggplot(iris, aes(x=Species, y=Petal.Width)) +
geom_bar(stat='identity')+coord_flip(clip='off')+
annotation_custom(grob = linesGrob(), xmin = 2.5, xmax = 2.5, ymin = -90, ymax = 0)+
annotation_custom(grob = linesGrob(), xmin = 1.5, xmax = 1.5, ymin = -90, ymax = 0)