5

I have an issue with geom_ribbon and I think this is a bug and not a feature. I want to zoom in on the "interesting" part of my plot but I don't want ggplot to exclude anything just because the entire thing doesn't fit into the plot. For that I use coord_cartesian to do the limiting. And it works for lines and points and probably many other things (bars) but not for geom_ribbon. So here's an example:

# Load libraries
library(ggplot2)

# Create data:
set.seed(1234)
LineA=c(seq(1,20,0.1))
LineB=c(seq(1,25,0.1))
LineC=c(seq(1,19,0.1))
LineD=c(seq(1,60,0.1))

my_df=data.frame(Mean = c(sort(sample(LineA,40)),sort(sample(LineB,40)),sort(sample(LineC,40)), 
                           sort(sample(LineD,40))))

my_df$Names=c(rep("Line-A",40),rep("Line-B",40),rep("Line-C",40),rep("Line-D",40))
my_df$SD=c(runif(n = 120, min = 1, max = 5),runif(n = 40, min = 1, max = 20))
my_df$Time=c(1:40,1:40,1:40,1:40)
my_df$Mean_low=my_df$Mean-my_df$SD
my_df$Mean_low[my_df$Mean_low<0]=0
my_df$Mean_hi=my_df$Mean+my_df$SD
head(my_df)

# Plot
# Ribbon visible:
ggplot(my_df, aes(x=Time, y=Mean)) + geom_line(aes(colour = Names), size = 1) +
    geom_point(size = 2, aes(shape = Names, color = Names))+
    geom_ribbon(aes(x = Time, y=NULL, ymin = Mean_low, ymax = Mean_hi, fill = Names),
                show.legend = F, linetype = 0, alpha = 0.1, na.rm = T) +
    geom_hline(yintercept = 20, linetype = "dotdash", color = "red", size = 1)+
    theme_classic()+
    scale_y_continuous("Mean value", breaks = seq(0, 100, 2), expand = expansion(mult = c(0, 0.01))) +
    scale_x_continuous("Days", breaks = seq(0, max(my_df$Time),2),
                       expand = expansion(mult = c(0.01, 0.005))) +
    coord_cartesian(ylim = c(0, 100), xlim = c(0, 50))

ribbon visible Here the ribbon visible if all of it is allowed to fit in the plot but the Ribbon is missing for Line-D completely when I limit the y axis as seen here below:

ggplot(my_df, aes(x=Time, y=Mean)) + geom_line(aes(colour = Names), size = 1) +
    geom_point(size = 2, aes(shape = Names, color = Names))+
    geom_ribbon(aes(x = Time, y=NULL, ymin = Mean_low, ymax = Mean_hi, fill = Names),
                show.legend = F, linetype = 0, alpha = 0.1, na.rm = T) +
    geom_hline(yintercept = 20, linetype = "dotdash", color = "red", size = 1)+
    theme_classic()+
    scale_y_continuous("Mean value", breaks = seq(0, 100, 2), expand = expansion(mult = c(0, 0.01))) +
    scale_x_continuous("Days", breaks = seq(0, max(my_df$Time),2),
                       expand = expansion(mult = c(0.01, 0.005))) +
    coord_cartesian(ylim = c(0, 30), xlim = c(0, 50))

no ribbon

I found only one workaround as also described here: Extended range in geom_ribbon by manually removing the data (NA for values) for values that would stay outside limits but that is a workaround and not a solution. The limiting/zooming works for most other geom options, then why not for the geom_ribbon as well? Does anyone know a more elegant solution? Is it a bug? Should I try to let ggplot people know?

Thank you!!

Gunnar
  • 97
  • 5
  • 1
    Not reproducible. When I run your second `ggplot`, the ribbon for line D is visible as expected. – Limey Sep 07 '21 at 12:16
  • Your code works fine on my Mac using `R 4.1.0` and `ggplot2 3.3.5`. – stefan Sep 07 '21 at 12:17
  • My r version is: major 4 minor 1.0. and my ggplot is 3.3.5 running Win10. Even tried updating it just now and still no fix. Have you guys tried zooming-in in R studio and/or exporting the plot as an image or vector graphics? – Gunnar Sep 07 '21 at 13:48
  • @Gunnar: I'm having the same problem; I think it *is* a problem with ggplot2. – shirewoman2 Feb 02 '22 at 20:49
  • Same problem here and indeed the answer of skshitij is right : if you save (with png()), the zoom is correctly done (but not with ggsave())... It should be a problem of version somewhere, that didn't exist before – Chika Jun 15 '22 at 13:58

1 Answers1

0

Just installing the ragg library [library(ragg)] displays the ribbons when the plot is exported/saved. The cut off bands are still not visible when zooming-in in R-studio plot, though. It could be a bug in the ggplot.

skshitij
  • 9
  • 2
  • 2
    Please add some reproducible code, with an image of the output if possible. – InverniE Dec 13 '21 at 22:11
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 22:14
  • This solution does not work for me, running a windows machine. I also tried the `ragg::agg_jpeg` function to no avail – dandrews Mar 24 '23 at 21:04