我想在ggplot中显示降水过剩的时间序列。我很漂亮,但我的丝带着色问题。我的数据包括多年的降水量过剩日数(实际上它是10天的平均值,但超出了这个问题)。这里是我的数据样本(2个月),带有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)
使用this SO-question我写了以下代码:
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")
这里的问题当然是&#39; slivers&#39;显示在x轴的另一侧。这是错误的,因为有盈余或赤字,但从来没有同时出现。我怀疑它发生了,因为每一个剩余的观察都与下一个相关。但在我的data.frame中,值为0,所以我不明白为什么。
我也尝试将色带分成正片和负片。这样做效果更好,但是丝带仍然显示出轻微的重叠并穿过黑线:
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")
我的问题是: 1.为什么第一段代码不起作用? 2.第二个代码是最佳解决方案还是有更好的方法?提前谢谢!