1

I'm making a Shiny app, and I'm using geom_line(aes(x= x, y =y, group = g, colour = g), size=2, na.rm=T) and then geom_point(aes(x= x, y = x, group = g, colour = g), size=5, na.rm=T, shape=16) and I have the line of one group (here in green) behind the points but above the line of an other group (here in pink).

enter image description here


Code:

library(ggplot2)
ggplot(data=df2, aes(x=DateRelCases, y = CasesCumMod)) +
  geom_line(aes(x= DateRelCases, y =CasesCumMod, group = Country, colour = Country), size=2, na.rm=T) +
  geom_point(aes(x= DateRelCases, y =CasesCumMod, group = Country, colour = Country),size=5, na.rm=T, shape=16)

Data:

df2 <- data.frame(Country = structure(c(rep(2L, 30), rep(3L, 30), rep(1L, 30)), .Label = c("ES", "FR", "IT"), class = "factor"), 
                      DateRelCases = structure(rep(30:1,3)), 
                      CasesCumMod = c(40174, 37575, 32964, 29155, 25233, 22302, 
                                      19856, 16018, 14459, 12612, 10995, 9134, 7730, 6633, 5423, 
                                      4499, 3661, 2876, 2281, 1784, 1412, 1126, 716, 613, 423, 
                                      285, 212, 178, 130, 100, 97689, 92472, 86498, 80539, 74386, 
                                      69176, 63927, 59138, 53578, 47021, 41035, 35713, 31506, 27980, 
                                      23980, 17750, 17660, 15113, 12462, 10149, 9172, 7375, 5883, 
                                      4636, 3858, 3089, 2502, 1835, 1689, 1128, 78797, 72248, 64059, 
                                      56188, 47610, 39673, 33089, 28572, 24926, 19980, 17147, 13716, 
                                      11178, 9191, 7753, 5753, 4231, 3004, 2140, 1639, 1204, 589, 
                                      430, 374, 261, 200, 151, 114, 83, 66))
r2evans
  • 141,215
  • 6
  • 77
  • 149
Lo Bo
  • 537
  • 1
  • 4
  • 15
  • 4
    (1) Is this specific to within the shiny app, or does it do it on the console as well? (2) Without something reproducible, how can we be of assistance? – r2evans Mar 31 '20 at 18:14
  • @r2evans: I was able to make a MWE for the console, see my update – Lo Bo Mar 31 '20 at 19:15
  • 1
    Most of that code appears to be unrelated to your question of layering in `ggplot2`. Have you tried the approach in my answer? – r2evans Mar 31 '20 at 19:17
  • @r2evans "Most of that code appears to be unrelated to your question of layering in ggplot2" Yes because I tried to reproduce it with fake data but couldn't. I thought someone might understand why points get on the top of lines like this and help me – Lo Bo Mar 31 '20 at 19:21
  • 1
    See my suggested edit. *That* is a lot closer to a MWE, since we don't need (or care) to know how you got the data. In the spirit of MWE, though, can you reproduce without `ggdark`? It's quite simple for people who have ***just*** `ggplot2` installed to just give this a try, but the momeny errors start popping up (like `could not find function "dark_theme_dark"`), you start losing people's interest. – r2evans Mar 31 '20 at 19:58
  • (For the record, MWE does not always mean "fake data". It just means small enough to be "not huge" in the question, with enough variability to break out the grouping and problem as you see it ... *representative* of what you need. In this case, the whole dataset is not too big, so `dput` is sufficient. Arguably, though, perhaps I could have removed all data after March 16, which would have halved the data and still shown the overlap you are trying to get across. – r2evans Mar 31 '20 at 20:17
  • 1
    Thanks a lot, I did not know `dput` and will make use of it in the future. I removed all unnecessary code – Lo Bo Mar 31 '20 at 20:19
  • 1
    Perhaps late to the discussion, but some good references for making questions *just big enough* (and good) in ways that encourage easy perusal and "trying" for potential answers: https://stackoverflow.com/questions/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Hope my answer addressed your question! You have had several questions (and answers), so I did not jump to that list of references right away, as I typically do with new SO users. – r2evans Mar 31 '20 at 20:24

1 Answers1

1

Layering is impacted first by the order of function calls (geom_line before and therefore under geom_point), and second by the factors (levels) within the data (when using group= and other grouping aesthetics). This is why all dots are layered on top of lines.

To add red-line, red-point, blue-line, blue-point (or whatever the order of colors is), you need to do it explicitly in the order you intend/need. This breaks some programmability of it, to be clear, but you get the layering you want. (You can affect the order of cntry by some metric if needed, so it is still programmable ... just not easily within the ggplot2 parlance.)

gg <- ggplot(data=df2, aes(x=DateRelCases, y = CasesCumMod)) + ggdark::dark_theme_dark()
for (cntry in unique(df2$Country)) {
  gg <- gg +
    geom_line(aes(x = DateRelCases, y =CasesCumMod, group = Country, colour = Country),
              size = 1, na.rm = TRUE, data = df2[df2$Country == cntry,]) +
    geom_point(aes(x = DateRelCases, y =CasesCumMod, group = Country, colour = Country),
               size = 3, shape = 16, na.rm = TRUE, data = df2[df2$Country == cntry,])
}

(I shrunk the sizes a little for this smaller image, it works the same with your original sizes.)

ggplot2 with correct layers

r2evans
  • 141,215
  • 6
  • 77
  • 149