2

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?

tjebo
  • 21,977
  • 7
  • 58
  • 94

1 Answers1

1

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.

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • 1
    Thanks Jared - actually, theme_minimal does the trick for my specific use case. Very helpful. I need to check what the exact settings are for this theme and why it doesn't work with other themes (I stumbled upon this on using `cowplot::theme_minimal_grid`)... – tjebo Apr 29 '21 at 00:25
  • 1
    I'll keep it open for now though in the hope to get some more attention... I am also curious if someone has another insight into the theme specs – tjebo Apr 29 '21 at 00:26
  • Happy for you to un-accept my answer and leave it open @tjebo - see if anyone else has a better idea - maybe put a bounty on it to get more exposure? – jared_mamrot Apr 29 '21 at 01:03
  • 1
    Thanks - I think you answered my particular question very well. Indeed, and fairly embarrassingly (it is late in the UK) I was not even thinking so far, and I did not understand that the behaviour was due to panel.border and axis.line. I think, however, I'll ask a follow up question and check if someone comes up with a way to actually draw an axis *below* the data points. Some wizard like teunbrand might be able to do so. I think this might work with guides manipulation – tjebo Apr 29 '21 at 01:06
  • I decided [to ask a new question](https://stackoverflow.com/questions/67334102/is-it-possible-to-draw-the-axis-line-first) – tjebo Apr 30 '21 at 12:41