使用ggplot2绘制引导程序样本(多列)

时间:2018-06-28 08:03:42

标签: r ggplot2

我正在尝试使用ggplot绘制引导程序样本(1000列),但结果数字不正确。

以下是数据:

dat<- structure(list(YY = c(1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 
1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L, 1961L), MM = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1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, 9L, 9L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L), DD = 1:365, RR = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 5.3, 15.7, 3.6, 11.7, 40.1, 0, 0, 34, 9.4, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16.3, 15, 0, 
0, 0, 4.6, 0, 0, 0, 5.3, 14.7, 7.1, 0, 4.1, 0.5, 0, 1.8, 1.3, 
0, 0, 0, 0, 0, 39.4, 1, 25.9, 0, 4.3, 4.6, 2, 9.7, 0, 0, 2, 0, 
0, 0.3, 57.9, 31, 2.3, 0, 1.8, 0, 0, 0, 0, 20.8, 11.7, 1, 3.1, 
0, 0, 0, 0.3, 8.6, 0, 0.3, 5.1, 0, 0, 0, 10.9, 7.1, 0, 4.1, 1.5, 
2.8, 15.8, 109.5, 140, 25.7, 57.2,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 119.8, 
119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 
119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 
119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 
119.8, 119.8, 119.8, 119.8, 119.8, 119.8, 136.1, 151.1, 151.1,   
151.1, 151.1, 155.7, 155.7, 155.7, 155.7, 161, 175.7, 182.8, 
182.8, 186.9, 187.4, 187.4, 189.2, 190.5, 190.5, 190.5, 190.5, 
190.5, 190.5, 229.9, 230.9, 256.8, 256.8, 261.1, 265.7, 267.7, 
277.4, 277.4, 277.4, 279.4, 279.4, 279.4, 279.7, 337.6, 368.6, 
370.9, 370.9, 372.7, 372.7, 372.7, 372.7, 372.7, 393.5, 405.2, 
406.2, 409.3, 409.3, 409.3, 409.3, 409.6, 418.2, 418.2, 418.5, 
423.6, 423.6, 423.6, 423.6, 434.5, 441.6, 441.6, 445.7, 447.2, 
450, 465.8, 575.3, 715.3, 741, 798.2, 807.6, 947.8, 1067.4, 1098.6, 
1099.4, 1143.3, 1146.1, 1198.4, 1243.9, 1244.7, 1257.1, 1286.1, 
1352.4, 1469, 1624.7, 1647.3, 1654.2, 1675.5, 1714.6, 1736.2, 
1756, 1756, 1770, 1770, 1781.2, 1781.2, 1795.4, 1807.1, 1859.9, 
1934.3, 1968.3, 1973.4, 1973.4, 1973.4, 1973.4, 1973.4, 1975.9, 
1976.2, 1982.8, 1988.9, 2020.7, 2027.6, 2035.2, 2037, 2043.6, 
2051, 2059.4, 2059.4, 2080.2, 2083.2, 2098.7, 2107.6, 2114, 2125.9, 
2137.6, 2264.6, 2364.9, 2421.3, 2427.4, 2455.8, 2540.4, 2573.7, 
2580.1, 2585.2, 2615.7, 2620.3, 2624.4, 2643.7, 2683.3, 2710.5, 
2718.4, 2722, 2725.3, 2735, 2745.7, 2747, 2806.7, 2858.3, 2924.1, 
2952.8, 2966, 2968.8, 3001.3, 3012, 3069.2, 3071.7, 3071.7, 3071.7, 
3101.2, 3101.2, 3101.2, 3107.8, 3139, 3150.7, 3152.2, 3152.2, 
3152.7, 3154.2, 3183.9, 3197.1, 3217.4, 3251.7, 3261.4, 3262.4, 
3263.9, 3268, 3281.5, 3314.5, 3314.5, 3314.5, 3325.4, 3326.9, 
3334.3, 3347.3, 3347.3, 3347.3, 3347.3, 3347.3, 3347.3, 3347.3, 
3347.3, 3347.3, 3347.3, 3347.3, 3347.3, 3347.3, 3352.6, 3352.6, 
3352.6, 3361.5, 3375.2, 3375.2, 3375.2, 3375.2, 3375.2, 3375.2, 
3375.2, 3393.5, 3393.5, 3393.5, 3393.5, 3393.5, 3393.5, 3393.5, 
3393.5, 3397.1, 3411.3, 3418.9, 3418.9, 3419.4, 3419.4, 3419.4, 
3419.4, 3419.4, 3419.4, 3419.4, 3419.4, 3419.4, 3419.4, 3426, 
3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 
3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 3426, 
3426, 3426, 3426, 3427)), .Names = c("YY", "MM", "DD", "RR", 
"CUMSUM"), class = "data.frame", row.names = c(NA, -365L))

这是我的剧本:

library(reshape2)
library(ggplot2)
library(meboot)

set.seed(1234)
dat[is.na(dat)]<- " "
out <- meboot(x=dat$CUMSUM,reps=1000,trim=0.10,elaps=TRUE)
f<-out$ens



Z<-out$ens
colnames(Z)<-c(seq(1:1000))
ZZ<-cbind(dat[3],Z)
df.melted<-melt(ZZ,id="DD")

p<-ggplot()
p <- p + geom_line(data=df.melted,aes(x=DD,y=value),color="black",alpha=0.1,size=0.5)

p <-p + geom_line(data=dat,aes(x=DD,y=CUMSUM),color="red",size=1)
p<-p + theme(panel.background=element_rect(fill="white"),
         plot.margin = unit(c(0.5,0.5,0.5,0.5),"cm"),
         panel.border=element_rect(colour="black",fill=NA,size=1),
         axis.line.x=element_line(colour="black"),
         axis.line.y=element_line(colour="black"),
         axis.text=element_text(size=15,colour="black",family="serif"),
         axis.title=element_text(size=15,colour="black",family="serif"),
         legend.position = "none")

p <- p + labs(x="Day of Year",y="Cummulative Rainfall (mm)")

outImg <- paste0("bootrapped",".png")
ggsave(outImg,p,width=8,height=5)

结果图像是这样的:

Output Image

问题/问题

  1. 我想通过ggplot绘制每列(1000列)并覆盖原始时间序列(红色)。但是,从图形上看,这些线(自举列)似乎相互连接。我希望线条更加清晰。我可能没有正确使用melt

关于如何在ggplot中正确执行此操作的任何建议? 我将不胜感激。

1 个答案:

答案 0 :(得分:5)

您需要按variable分组。

p <- p + geom_line(data=df.melted,aes(x=DD,y=value, group = variable),color="black",alpha=0.1,size=0.5)

enter image description here