Get break positions in ggplot

I created a function to create barchart with ggplot.

In my figure, I want to overlay a graph with white horizontal stripes in the position of the labels, as in the figure below

p <- ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
geom_bar(stat = 'identity')
# By inspection I found the y-tick postions to be c(50,100,150)
p + geom_hline(aes(yintercept = seq(50,150,50)), colour = 'white')

irisplot

However, I would like to be able to modify the data, so I cannot use static positions for strings, as in the example. For example, I can change Sepal.Withto Sepal.Heightin the above example.

Can you tell me how:

  • get tick positions from my ggplot; or
  • get the function that ggplotuses for the tick position so that I can use it to place my lines.

so i can do something like

tickpositions <- ggplot_tickpostion_fun(iris$Sepal.Width)
p + scale_y_continuous(breaks = tickpositions) +
geom_hline(aes(yintercept = tickpositions), colour = 'white')
+4
source share
3

(1) ggplot_build . ggplot_build "[...] , [...] breaks".

ggplot_build(p)$layout$panel_ranges[[1]]$y.major_source
# [1]   0  50 100 150

. pre- ggplot2 2.2.0 .

+7

ggplot2::ggplot_build - . . str() ggplot_build, , .

, , panel --> ranges --> y.major_source, , , , . , :

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


, ggplot geom_bar, geom_line , . - data = data.frame() geom_line; , , . - :)

+1

. seq().

seq(from = min(), to = max(), len = 5)

- .

p <- ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
geom_bar(stat = 'identity')
p + geom_hline(aes(yintercept = seq(from = min(), to = max(), len = 5)), colour = 'white')
-1

All Articles