0

Based on this example, I am trying to plot lines by group and varying linetype, and add under each subgroup a shade of color for that group. So, I have a group modif that I need to color the underlining ribbon, and have grp to specify linetype. Ideally, if lines would be black/have the same color as the ribbon (ribbon should have some alpha). AS my ribbon is not an indication of confidence interval, insteda it plots min and max values from additional groups, I use ~ pivot_wider and accordingly define ymin = c, ymax = a.

This is what I want to get, including the line plot and ribbons by group:

enter image description here

and this is what I get so far, without the underlying ribbons:

enter image description here

Here is my dummy example:

# Make a different colur of shade by group

library(ggplot2)
library(tidyr)

# example for shaded line plot
dd1 <- data.frame(year = c(1:5),
                 grp = rep(c("a", "b", "c"), each = 5),
                 vals = c(5, 5.2, 5.6, 5.8, 6,
                          5, 4.9, 4.8, 4.7, 4.2,
                          5, 4.8, 4.4, 4,   3),
                 modif = rep('no', each = 15))

dd2 <- dd1
dd2$vals = dd1$vals*0.8
dd2$modif = 'yes'

# Put data together
dd <- rbind(dd1, dd2)

# Get a plot
dd %>% 
  ggplot(aes(x = year)) +
 geom_ribbon(
  data = ~ pivot_wider(., names_from = grp,
                      values_from = vals),
  aes(ymin = c, ymax = a, fill = modif)
 ) +
  ylim(0,6.5) +
   geom_line(aes(y = vals,color = modif, linetype = grp), 
            lwd  = 1.5)  +
 
  theme_bw()

If I run the code, I got a warning message: Warning messages: 1: In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 2: In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf and geom_ribbon is missing. How to correct this?

maycca
  • 3,848
  • 5
  • 36
  • 67
  • 4
    Your code runs without errors for me and gives the expected output. Have you tried starting a fresh R session and running this code? – teunbrand Apr 22 '21 at 07:02
  • Thanks for your answer @teunbrand! hm, strange, I still have the same wrong output, after restarting R and even my PC... My R version is 3.6.1 (2019-07-05) on platform x86_64-w64-mingw32. Maybe i need some upgrade. Or do you have any suggestions why this is not working, and to what the error relates? – maycca Apr 22 '21 at 08:38
  • I think ggplot2 maintains reverse compatibility with the previous 3 or 4 minor versions of R , so R 3.6.1 should be good until R 4.2 or 4.3 comes around (probably next year). What version of ggplot2 are you using? Your example can be directly reprex'ed from the clipboard without modification and the error is not very descriptive, so I don't know what might be causing it. – teunbrand Apr 22 '21 at 10:10
  • Hi, my ggplot is ggplot2_3.3.2. I also tried to update my dplyr and tidyr libraries because of warning `Warning message: replacing previous import ‘vctrs::data_frame’ by ‘tibble::data_frame’ when loading ‘dplyr’` but seems not to helped. I have asked a colleagues (both are using same R, no admin rights due to work PCs) and it does not show the ribbon neither. I wonder what else to do? – maycca Apr 22 '21 at 10:20
  • 1
    OP, are you sharing the entire piece of code you're using that gives you the error? The error message is related to running `max()` method and returning `-Inf`. This represents the error when `max()` is sent an empty vector - in other words, just running `max()` in your console. This means that `max()` is being run somewhere in code sent an empty vector. Often something was filtered and sent to `max()`, but nothing was actually there... something like that, but I can't find anything related to that in your code. – chemdork123 Apr 22 '21 at 11:48
  • Well, @chemdork123, you are right... now it magically works... now idea why and how... anyway, thanks for checking it! maybe it was the `dplyr` update or something else... You are right, I was in some examples checking the `ymin = min(c)` and `ymax = max(a)` but I thought that I had this in both cases. Maybe just uncorrectly cleaned memory. – maycca Apr 22 '21 at 12:54

0 Answers0