Prevent theme from undesired drawing of axis on top of points and clip = "off"

Viewed 72

Maybe it is by design, to cut points neatly when clip = "on"?

How can I prevent it?

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

Created on 2021-04-29 by the reprex package (v2.0.0)

update

Jared's answer was very helpful to identify my thought problem. The different themes have different settings for panel.border and axis.line.

This still leaves the question - how to make the axis line draw below the data points?

1 Answers

As a workaround, I guess you could remove the axes and use the panel.grid as de-facto axes? E.g.

df <- data.frame(var = c(0, 1, 2, 3), val = c(0, 1, 2, 3))

ggplot(df) + 
  geom_point(aes(val, var), color = "red", alpha = 1, size = 6) +
  scale_x_continuous(expand = c(0, 0),
                     limits = c(0, 3)) +
  scale_y_continuous(expand = c(0, 0),
                     limits = c(0, 3)) +
  coord_cartesian(clip = "off") +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_line(color = "black"))

example_1.png

This is a very interesting question; I would like to see if anyone else can come up with a better alternative.

Related