I want to display a time-series with precipitation surplus in ggplot. I got pretty for but have an issue with the coloring of my ribbons. My data consist of many years with the daily number for precipitation surplus (actually it's a 10 day average but that is beyond this question). Here a sample of my data (2 months) with dput:
structure(list(X10Dsom = c(12.5, 13.1, 12.4, 12.9, 11.3, 3, -28.9,
-24.4, -25.9, -27.9, -25.8, -23.8, -22.8, -17, -14.6, -15.2,
-15.4, -14.9, -14.3, -8.4, -8.5, -6.9, -7, -10.1, -7.3, -8.4,
-10.5, -13.9, -14.8, -21.1, -20.6, -21.7, -20.4, -22.3, -26.8,
-26.7, -25.8, -23, -18.6, -11.8, 13.9, 36.8, 43.3, 46.9, 58,
60.3, 66.8, 67.9, 72.3, 77, 55.9, 33, 27.5, 25.3, 16.2, 16.2,
10.3, 6.2, -1.7, -12.8, -17.1), CS_datumDMY = structure(c(1375315200,
1375401600, 1375488000, 1375574400, 1375660800, 1375747200, 1375833600,
1375920000, 1376006400, 1376092800, 1376179200, 1376265600, 1376352000,
1376438400, 1376524800, 1376611200, 1376697600, 1376784000, 1376870400,
1376956800, 1377043200, 1377129600, 1377216000, 1377302400, 1377388800,
1377475200, 1377561600, 1377648000, 1377734400, 1377820800, 1377907200,
1377993600, 1378080000, 1378166400, 1378252800, 1378339200, 1378425600,
1378512000, 1378598400, 1378684800, 1378771200, 1378857600, 1378944000,
1379030400, 1379116800, 1379203200, 1379289600, 1379376000, 1379462400,
1379548800, 1379635200, 1379721600, 1379808000, 1379894400, 1379980800,
1380067200, 1380153600, 1380240000, 1380326400, 1380412800, 1380499200
), tzone = "UTC", class = c("POSIXct", "POSIXt"))), .Names = c("X10Dsom",
"CS_datumDMY"), class = "data.frame", row.names = 4962:5022)
Using this SO-question I wrote the following code:
library(ggplot2)
library(lubridate)
library(dplyr)
library(data.table)
#create a index for surplus or deficit
df_meteo$nat <- as.factor(cut(df_meteo$X10Dsom, breaks=c(-50,0,100), labels=FALSE))
#calculate ymax and ymin for ribbon
setDT(df_meteo)[, ymax:=pmax(0,X10Dsom)]
setDT(df_meteo)[, ymin:=pmin(0,X10Dsom)]
#and plot
ggplot(df_meteo, aes(x=df_meteo$CS_datumDMY, y=df_meteo$X10Dsom)) +
geom_ribbon(aes(y=0,ymin=ymin,ymax=ymax,group=df_meteo$nat,fill=df_meteo$nat)) +
geom_line(color="black")
The issue here of course is the 'slivers' that show on the opposite side of the x-axis. This is wrong, because there is a surplus or a deficit, but never both a the same time. I suspect it happens because every last observation with a surplus is connected to the next. But in my data.frame, the value is 0, so i don't understand why.
I have also tried splitting the ribbon into a positive and a negative ribbon. That works better, but the ribbons still show slight overlap and cross the black line:
ggplot(df_meteo, aes(x=df_meteo$CS_datumDMY, y=df_meteo$X10Dsom)) +
geom_ribbon(aes(y=0,ymin=ymin,ymax=0,fill="red")) +
geom_ribbon(aes(y=0,ymin=0,ymax=ymax,fill="blue")) +
geom_line(color="black")
My questions are: 1. Why doesn't the first piece of code work? 2. Is the second code the best solution available or is there a better way? Thank you in advance!