创建数据框的子集以指示 geom_ribbon 中的限制

Create subset of data frame to indicate limits in geom_ribbon

我试图在图中的两条线之间填充,但经过多次试验后,我仍然无法弄清楚如何在代码中显示我需要着色的特定区域。

这是我的数据集:

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

这是我的代码:

(我发布了完整的代码以备不时之需,但请忽略与问题没有直接关系的部分)。

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)

我需要将阴影区域放置在 HC 和 HCc 线之间。 .

如何在代码中显示上限为HC均值,下限为HCc均值?

我用这些值创建了分离向量,但代码产生错误。

我认为您需要将不同的数据框以宽格式传递到功能区:

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)