Is it possible to draw the axis line first, before the data?

9

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.

Share
Improve this question
3
  • This is likely not waht you want but how about changing the "z" column in the gtable i.e. 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
  • @user20650 yes and no. I think this goes very much in the right direction. I'd generally prefer a more "on the draw level solution", actually mainly out of curiosity. 2) Not sure why though, but when I try that swapping of the z column, the appearance of the axis changes. Might be a device thing. Right now about time to go to bed here, will need to dig into that tomorrow. Thanks already! – tjebo May 3 at 0:43
  • Yes, i think the axis lines look a bit thinner ... possible as the panel is now getting draw over part of them? – user20650 May 3 at 0:52

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue