I am analyzing monthly observations of water input (rainfall) and output (evaporation) at a given location.
I need to plot time series of both rainfall and evaporation, shading the area between the data points with varying colors according to which line is above the other.
This is what I have:
library(ggplot2)
library(reshape2)
dat1 <- structure(list(month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), value = c(226.638505697305, 186.533910906497, 141.106702957603,
93.4474376969313, 134.58903301495, 77.6436398653559, 77.301864710113,
69.7349071531699, 109.208227499776, 165.197186758555, 156.057081859175,
168.342059689587, 136.34266772667, 119.741309096806, 120.395245911241,
98.1418096019397, 72.4585192294772, 59.6209861948614, 69.6993145911677,
97.1585171469416, 118.357052089691, 132.74037278737, 139.141233379528,
146.583047731729), var = c("rainfall", "rainfall", "rainfall",
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall",
"rainfall", "rainfall", "rainfall", "evaporation", "evaporation",
"evaporation", "evaporation", "evaporation", "evaporation", "evaporation",
"evaporation", "evaporation", "evaporation", "evaporation", "evaporation"
)), row.names = c(NA, -24L), class = "data.frame")
ggplot(dat1, aes(x=month,y=value, colour=var)) +
geom_line() +
scale_color_manual(values=c("firebrick1", "dodgerblue")) +
theme_bw(base_size=18)
which yields the following graph (with little edits to show what I'm trying to achieve):
My initial attempt to fill the areas between the lines was based on this SO answer:
dat2 <- data.frame(month=1:12,
rainfall=dat1[dat1$var=="rainfall",]$value,
evaporation=dat1[dat1$var=="evaporation",]$value)
dat2 <- cbind(dat2, min_line=pmin(dat2[,2],dat2[,3]) )
dat2 <- melt(dat2, id.vars=c("month","min_line"), variable.name="var", value.name="value")
ggplot(data=dat2, aes(x=month, fill=var)) +
geom_ribbon(aes(ymax=value, ymin=min_line)) +
scale_fill_manual(values=c(rainfall="dodgerblue", evaporation="firebrick1"))
However, it's not quite what I need.
How can I achieve the desired result?