ggplot2: How to get different color ribbons above

2019-07-22 08:18发布

问题:

I have a data to make a geom_ribbon and then a geom_line over it. And now, I would like to make the part of the ribbon above the red line, coloured differently.

Data data for the plot:

#simulate data
dat <- data.frame(1:12)
colnames(dat) <- c("code")
dat$code <- month.abb
dat$code <- factor(dat$code, levels = dat$code)
dat$Th <- c(42, 44, 53, 64, 75, 83, 87, 84, 78, 67, 55, 45)
dat$Tl <- c(27, 28, 35, 44, 54, 63, 68, 66, 59, 48, 38, 29)
dat$prec <- c(3.03, 2.48, 3.23, 3.15, 4.13, 3.23, 4.13, 4.88, 3.82, 3.07, 2.83, 2.8)
dat$prec <- dat$prec*16
dat

#plot
ggplot(data = dat, aes(x = code, ymin = Tl, ymax = Th)) +
  geom_ribbon(group = 1) +
  geom_line(data = dat, aes(x = code, y = prec, group = 2), colour = "red", size = 3) +
  expand_limits(y = 0)

回答1:

We can do this by creating two ribbons, one between Tl and prec and one between prec and Th. In each case, we also need to address the case where prec is below or above, respectively, both Th and Tl:

ggplot(dat, aes(x = code)) +
  geom_ribbon(aes(ymin=pmin(pmax(prec,Tl),Th), ymax=Th, group=1), fill="blue") +
  geom_ribbon(aes(ymin=Tl, ymax=pmax(pmin(prec,Th),Tl), group=2), fill="green") +
  geom_line(aes(y = prec, group = 2), colour = "red", size = 3) +
  expand_limits(y = 0)

But note that the plot is not quite right because of the limited horizontal resolution of the data:

So let's create a new data frame that linearly interpolates the actual data at a bunch of points between each month and create the plot again:

dat.new = cbind.data.frame(
  code=seq(1,12,length=200), 
  sapply(dat[,c("prec","Th","Tl")], function(T) approxfun(dat$code, T)(seq(1,12,length=200)))
  )

ggplot(dat.new, aes(x = code)) +
  geom_ribbon(aes(ymin=pmin(pmax(prec,Tl),Th), ymax=Th, group=1), fill="blue") +
  geom_ribbon(aes(ymin=Tl, ymax=pmax(pmin(prec,Th),Tl), group=2), fill="green") +
  geom_line(aes(y = prec, group = 2), colour = "red", size = 3) +
  expand_limits(y = 0) +
  scale_x_continuous(breaks=1:12, labels=month.abb)



标签: r ggplot2