Get tick break positions in ggplot
A possible solution for (1) is to use ggplot_build
to grab the content of the plot object. ggplot_build
results in "[...] a panel object, which contain all information about [...] breaks".
ggplot_build(p)$layout$panel_ranges[[1]]$y.major_source
# [1] 0 50 100 150
See edit for pre-ggplot2 2.2.0
alternative.
Check out ggplot2::ggplot_build
- it can show you lots of details about the plot object. You have to give it a plot object as input. I usually like to str()
the result of ggplot_build
to see what all the different values it has are.
For example, I see that there is a panel --> ranges --> y.major_source
vector that seems to be what you're looking for. So to complete your example:
p <- ggplot() +
geom_bar(data = iris, aes(x = Species, y = Sepal.Width), stat = 'identity')
pb <- ggplot_build(p)
str(p)
y.ticks <- pb$panel$ranges[[1]]$y.major_source
p + geom_hline(aes(yintercept = y.ticks), colour = 'white')
Note that I moved the data argument from the main ggplot
function to inside geom_bar
, so that geom_line
would not try to use the same dataset and throw errors when the number in iris is not a multiple of the number of lines we're drawing. Another option would be to pass a data = data.frame()
argument to geom_line
; I cannot comment on which one is a more correct solution, or if there's a nicer solution altogether. But the gist of my code still holds :)
For ggplot 3.1.0 this worked for me:
ggplot_build(p)$layout$panel_params[[1]]$y.major_source
#[1] 0 50 100 150