Is it possible to draw the axis line first, before the data?
This is a follow up to my previous question where I was looking for a solution to get the axis drawn first, then the data. The answer works for that specific question and example, but it opened a more general question how to change the plotting order of the underlying grobs. First the axis, then the data.
Very much in the way that the panel grid grob can be drawn on top or not.
Panel grid and axis grobs are apparently generated differently - axes more as guide objects rather than "simple" grobs. (Axes are drawn with ggplot2:::draw_axis()
, whereas the panel grid is built as part of the ggplot2:::Layout
object).
I guess this is why axes are drawn on top, and I wondered if the drawing order can be changed.
# An example to play with
library(ggplot2)
df <- data.frame(var = "", val = 0)
ggplot(df) +
geom_point(aes(val, var), color = "red", size = 10) +
scale_x_continuous(
expand = c(0, 0),
limits = c(0,1)
) +
coord_cartesian(clip = "off") +
theme_classic()
P.S. I will not accept nor even upvote answers that create fake axes, because this is not what I am looking for or trying to understand.
g = ggplotGrob(p) ; g$layout[g$layout$name == "panel", "z"] = 12 ; g$layout[g$layout$name == "ylab-l", "z" ] = 0 ; grid::grid.draw(g)
– user20650 May 3 at 0:33