Home > Software design >  Create subset of data frame to indicate limits in geom_ribbon
Create subset of data frame to indicate limits in geom_ribbon

Time:03-20

I am trying to fill between two lines in a plot but after several trials I still can´t figure out how to show in the code the specific area that I need to be shaded.

Here´s my dataset:

high
Time Trat mean sd level
T1 HC 27.26 6.19 high
T1 Control HC 28.22 1.82 high
T1 HCc 26.39 6.19 high
T2 HC 32.81 0.84 high
T2 Control HC 30.10 2.58 high
T2 HCc 30.88 0.84 high
T3 HC 35.15 3.93 high
T3 Control HC 32.60 0.75 high
T3 HCc 33.91 3.93 high
T4 HC 38.60 3.50 high
T4 Control HC 29.66 4.81 high
T4 HCc 37.14 3.50 high
T5 HC 41.71 1.47 high
T5 Control HC 29.97 4.75 high
T5 HCc 39.78 1.47 high
T6 HC 41.07 0.32 high
T6 Control HC 27.04 4.79 high
T6 HCc 37.73 0.32 high
T7 HC 43.55 0.71 high
T7 Control HC 25.48 4.75 high
T7 HCc 40.32 0.71 high
T8 HC 45.71 2.62 high
T8 Control HC 24.13 4.61 high
T8 HCc 41.33 2.62 high
T9 HC 47.57 1.65 high
T9 Control HC 23.23 5.44 high
T9 HCc 42.12 1.65 high
T10 HC 51.53 4.40 high
T10 Control HC 24.69 6.81 high
T10 HCc 46.09 4.40 high

And here´s my code:

(I posted the full code just in case it´s needed but please ignore the parts that are not directly related to the question).

ggplot(high, aes(Time, mean, color=Trat,group = Trat)) 
geom_line( aes( linetype=Trat, color=Trat), size=1.5, na.rm=T) 
    labs(x = ("Time (days)"), y = bquote("Clorophyll-a"~ (µg.L^-1)))  
  scale_x_discrete(breaks=c("T1", "T2","T3", "T4", "T5","T6","T7","T8","T9","T10"),labels=c("1","2","3","4","5","6","7","8","9","10")) 
  theme_classic(base_size = 15) 
  theme(plot.title = element_text(hjust = 0.5)) 
  theme(legend.position="right") 
  theme(legend.title = element_blank()) 
  guides(col = guide_legend(nrow = 2)) 
  scale_color_grey() 
  geom_errorbar(aes(ymin=mean-sd, ymax=mean sd), width=1, position=position_dodge(.01)) 
  guides(colour=guide_legend(nrow=3)) 
  geom_ribbon(aes(x=Time, ymax=x.upper, ymin=x.lower), fill="gray", alpha=.5)

I need the shaded area to be placed between HC and HCc lines. . enter image description here

How can I show in the code that the upper limit are HC mean values and the lower limit are HCc means?

I created separated vectors with the values but the code produces an error.

CodePudding user response:

I think you'll need to pass a different data frame to the ribbon in wide format:

ggplot(high, aes(Time, mean)) 
geom_line( aes( linetype=Trat, color=Trat, group = Trat), size=1.5, na.rm=T) 
    labs(x = ("Time (days)"), y = bquote("Clorophyll-a"~ (µg.L^-1)))  
  scale_x_discrete(breaks=c("T1", "T2","T3", "T4", "T5","T6","T7","T8","T9","T10"),
                   labels=c("1","2","3","4","5","6","7","8","9","10")) 
  theme_classic(base_size = 15) 
  theme(plot.title = element_text(hjust = 0.5)) 
  theme(legend.position="right") 
  theme(legend.title = element_blank()) 
  guides(col = guide_legend(nrow = 2)) 
  scale_color_grey() 
  geom_errorbar(aes(ymin=mean-sd, ymax=mean sd,
                    color=Trat,group = Trat), 
                width=1, position=position_dodge(.01)) 
  guides(colour=guide_legend(nrow=3)) 
  geom_ribbon(data = tidyr::pivot_wider(high,
                                        id_cols = c("Time", "Trat"), 
                                        names_from = "Trat", 
                                        values_from = c("mean", "sd")),
              aes(y = 1, ymax=mean_HCc, ymin=mean_HC, color = "HC", group = "HC"), 
              fill="gray", alpha=.5)

enter image description here

  • Related