Can we add shadows to (dashed) lines in R (ggplot2

2019-07-11 05:58发布

问题:

I was asked to add shadows to a dashed-line in R. It would be similar to this image I found on-line, except it is for non-solid lines:

I did not find any specific control/package for this purpose. If it was a solid line that I needed a shadow for, I would have simulated the shadow using another line with different opacity and thickness. But it seems for the non-solid lines (dashed, dotted, etc) this workaround won't work, since their gaps won't match as you change the thickness.

Any ideas?


Image is copied from here only for the sake of clarity.

回答1:

You can only tweak this like

df <- data.frame(x = 1:20, y = cumsum(rnorm(20)))
ggplot(df, aes(x = x, y = y)) +
  geom_line(aes(x = x+0.15, y = y-0.15), alpha = 0.2, lwd = 1.2, linetype = 2) +
  geom_point(aes(x = x+0.15, y = y-0.15), alpha = 0.1, size = 2.5) +
  geom_line(lwd = 1, linetype = 2) + 
  geom_point(size = 2.5) +
  theme_bw()