Given the following plot:
library(tidyverse)
p <- ggplot(mtcars, aes(drat, disp)) +
geom_line()
p
layer_scales
can be used (here) to extract breaks/break positions from most ggplot
objects like the one above e.g.
# layer_scales(p)$y$get_breaks()
as.numeric(na.omit(layer_scales(p)$y$break_positions()))
# [1] 100 200 300 400
# returns exactly the breaks that are in the plot
But when I try to extract the ones from this plot, it doesn't work
df <- structure(list(date = structure(c(18080, 19281, 19096, 17178,
17692, 18659, 17129, 17114, 18833, 16472), class = "Date"), yy = c(1589L,
5382L, 4504L, 595L, 1027L, 2864L, 556L, 549L, 3346L, 42L)), class = "data.frame", row.names = c(NA,
-10L))
df
p1 <- ggplot(df, aes(x = date, y = yy)) +
geom_point()
p1
layer_scales(p1)$y$get_breaks()
# [1] 0 1000 2000 3000 4000 5000
as.numeric(na.omit(layer_scales(p1)$y$break_positions()))
# [1] 1000 2000 3000 4000 5000
# it doesn't return 0 2000 4000
Any idea why layer_scales
is not working in this case?
layer_scale()
doesn't work or to draw more attention to your question. This is an interesting one. Cheers. – Motalayer_scales
doesn't work though, I wondered had it something to do with using a date variable and some scaling issue (although that is in the other axis so probably not!). I will use your approach for now. – Burthen