如何填补/阴影时间线之间的空间,用ggplot绘制? [英] How to fill/shade space between timelines, plotted with ggplot?

查看:187
本文介绍了如何填补/阴影时间线之间的空间,用ggplot绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  monat beob werex_00 werex_11 werex_22 werex_33 werex_44 werex_55 werex_66 werex_77 werex_88 werex_99 Min最大
1 22.4930171 9.1418697 8.1558828 8.0312839 10.013298 8.8922567 9.395811 10.7933080 6.5136136 8.721697 10.279974 0.108381 59.65309
2 25.1414834 13.5886794 9.1694683 10.8709352 13.021066 10.3316655 10.579970 17.0555902 7.5915886 11.035921 13.366310 0.924013 66.94970
3分配33.8286673 16.3800292 10.0202342 11.3072626 17.674761 16.1370288 15.018551 15.3331395 12.6856599 15.479521 13.929905 -0.794309 78.78572
4 22.0579421 11.9930633 8.4899130 8.2304118 12.987301 7.8763578 8.554007 12.4956321 9.4723508 7.057423 7.688662 -10.496481 49.01380
5 2.5535161 -2.4503375 -4.2354520 -3.6309377 -2.969866 -4.5876993 -5.383716 -3.2612018 -5.2054387 -2.780719 - 4.35 9513 -19.579135 32.54282
6 -2.4405826 -8.8534136 -9.4666674 -7.4249244 -7.820072 -9.1485440 -8.546798 -7.8179739 -7.4222923 -10.978398 -12.644807 -22.821617 18.62139
7 -2.2580848 -6.7569968 -8.3390114 -8.8757506 -8.248305 - 8.4171552 -7.760800 -5.7471163 -8.7864075 -6.239596 -8.870658 -22.933219 20.84375
8 -0.3448858 -5.6683742 -5.0467756 -5.7201820 -2.800106 -5.9640095 -5.011171 -3.3557601 -2.8967683 -4.407761 -6.146411 -17.042893 17.86556
9 3.3963303 0.4305926 -0.8554308 -0.9985536 -1.184610 -0.5520555 0.347758 -0.3838614 -0.2199835 -1.174712 -1.630363 -8.533647 19.66163
10 5.1839209 1.6050281 1.1578316 1.8503193 2.327975 1.6633771 1.557532 1.5563157 2.2776155 1.667714 1.333829 -4.686715 31.17342
11分配9.2551418 4.4810518 2.9992301 4.9848408 3.824927 4.2413024 3.939119 5.4256008 3.5804488 4.965302 3.790589 -1.615777 43.90991
12分配18.2233848 7.7648233 6.3344735 7.3477135 6.573620 7.1884950 7.428654 7.3119002 6.9405167 7.663072 8.342437 0.014096 62.83760

这是时间一定价值的线。在下一步中,我用 ggplot()来绘制它。因此,我使用 melt()操作来获得绘制的矩阵:

  R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif<  - 熔融(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean,na.rm = FALSE,id.vars = monat)

现在这个数据如下:

  Monat Projektion值
1 1 beob 22.4930171
2 2 beob 25.1414834
3 3 beob 33.8286673
4 4贝宝22.0579421
5 5贝宝2.5535161
6 6贝宝-2.4405826
7 7贝宝-2.2580848
8 8 beob -0.3448858
9 9 beob 3.3963303
10 10 beob 5.1839209
11 11 beob 9.2551418
12 12贝宝18.2233848
13 1 werex_00 9.1418697
14 2 werex_00 13.5886794
15 3 werex_00 16.3800292
16 4 werex_00 11.9930633
17 5 werex_00 -2.4503375
18 6 werex_00 -8.8534136
19 7 werex_00 -6。 7569968
20 8 werex_00 -5.6683742
21 9 werex_00 0.4305926
22 10 werex_00 1.6050281
23 11 werex_00 4.4810518
24 12 werex_00 7.7648233
25 1 werex_11 8.1558828
... ... ... ...

我还添加了一些新的(如上所示):

 名称(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif)< -c(Monat, Projektion,value)

下一步为该图定义一些自定义颜色:

  Projektionen_Farben< -c(#000000,#00EEEE,#EEAD0E,#006400,#BDB76B, #EE7600,#68228B,#8B0000,#1E90FF,#EE6363,#556B2F,## > 



现在我绘制融化的数据:

  ggplot(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif,
AES(X = Monat,Y =值,颜色= Projektion,基团= Projektion))+
geom_line(大小= 0.8)+
xlab(Monat)+
ylab(Grundwasserneubildung [mm / Monat])+
ggtitle(Grundwasserneubildung)+
theme_bw()+
scale_x_continuous c(1,2,3,4,5,6,7,8,9,10,11,12),
labels = c(Jan,Feb,Mär,Apr ,Mai,Jun,Jul,Aug,Sep,Okt,Nov,Dez))+
theme(axis.title = element_text(size = 15 ,vjust = 0.3,face =bold),
title = element_text(size = 15,vjust = 1.5,face =bold))+
scale_colour_manual(values = Projektionen_Farben)

抱歉,我没有足够的声望发布图片。
现在我想填充/遮蔽最大线和最小线之间的空间,可以说是浅灰色(alpha = .3)。我尝试过使用 geom_ribbon(),但没有找到正确的方法来定义 x ymin ymax 。是否有人知道填补这两行之间空间的方法? 使用原始数据框作为 geom_ribbon(),并提供 Min
Max 作为 ymin ymax

  + geom_ribbon(数据= R1_Grundwasserneubildung_Rg1Rg2_Monat_mean,
AES(X = monat,YMIN =最小值,YMAX =最大值),
inherit.aes = FALSE,α-= 0.3,颜色= grey30)


I have a 12x13 matrix that looks like that:

monat   beob    werex_00    werex_11    werex_22    werex_33    werex_44    werex_55    werex_66    werex_77    werex_88    werex_99    Min Max
1   22.4930171  9.1418697   8.1558828   8.0312839   10.013298   8.8922567   9.395811    10.7933080  6.5136136   8.721697    10.279974   0.108381    59.65309
2   25.1414834  13.5886794  9.1694683   10.8709352  13.021066   10.3316655  10.579970   17.0555902  7.5915886   11.035921   13.366310   0.924013    66.94970
3   33.8286673  16.3800292  10.0202342  11.3072626  17.674761   16.1370288  15.018551   15.3331395  12.6856599  15.479521   13.929905   -0.794309   78.78572
4   22.0579421  11.9930633  8.4899130   8.2304118   12.987301   7.8763578   8.554007    12.4956321  9.4723508   7.057423    7.688662    -10.496481  49.01380
5   2.5535161   -2.4503375  -4.2354520  -3.6309377  -2.969866   -4.5876993  -5.383716   -3.2612018  -5.2054387  -2.780719   -4.359513   -19.579135  32.54282
6   -2.4405826  -8.8534136  -9.4666674  -7.4249244  -7.820072   -9.1485440  -8.546798   -7.8179739  -7.4222923  -10.978398   -12.644807 -22.821617  18.62139
7   -2.2580848  -6.7569968  -8.3390114  -8.8757506  -8.248305   -8.4171552  -7.760800   -5.7471163  -8.7864075  -6.239596   -8.870658   -22.933219  20.84375
8   -0.3448858  -5.6683742  -5.0467756  -5.7201820  -2.800106   -5.9640095  -5.011171   -3.3557601  -2.8967683  -4.407761   -6.146411   -17.042893  17.86556
9   3.3963303   0.4305926   -0.8554308  -0.9985536  -1.184610   -0.5520555  0.347758    -0.3838614  -0.2199835  -1.174712   -1.630363   -8.533647   19.66163
10  5.1839209   1.6050281   1.1578316   1.8503193   2.327975    1.6633771   1.557532    1.5563157   2.2776155   1.667714    1.333829    -4.686715   31.17342
11  9.2551418   4.4810518   2.9992301   4.9848408   3.824927    4.2413024   3.939119    5.4256008   3.5804488   4.965302    3.790589    -1.615777   43.90991
12  18.2233848  7.7648233   6.3344735   7.3477135   6.573620    7.1884950   7.428654    7.3119002   6.9405167   7.663072    8.342437    0.014096    62.83760

That are time-lines of a certain value. In the next step I plot it with ggplot(). Therefore I used the melt() operation to get the matrix in shape for plot:

R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif <- melt(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean, na.rm = FALSE, id.vars="monat")

This data looks like that now:

    Monat   Projektion  value
1   1   beob    22.4930171
2   2   beob    25.1414834
3   3   beob    33.8286673
4   4   beob    22.0579421
5   5   beob    2.5535161
6   6   beob    -2.4405826
7   7   beob    -2.2580848
8   8   beob    -0.3448858
9   9   beob    3.3963303
10  10  beob    5.1839209
11  11  beob    9.2551418
12  12  beob    18.2233848
13  1   werex_00    9.1418697
14  2   werex_00    13.5886794
15  3   werex_00    16.3800292
16  4   werex_00    11.9930633
17  5   werex_00    -2.4503375
18  6   werex_00    -8.8534136
19  7   werex_00    -6.7569968
20  8   werex_00    -5.6683742
21  9   werex_00    0.4305926
22  10  werex_00    1.6050281
23  11  werex_00    4.4810518
24  12  werex_00    7.7648233
25  1   werex_11    8.1558828
... ... ... ...

I also added some new names for the melted data (as already seen above):

names(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif)<-c("Monat","Projektion","value")

Next step defines some custom colors for the plot:

Projektionen_Farben<-c("#000000","#00EEEE","#EEAD0E","#006400","#BDB76B","#EE7600","#68228B","#8B0000","#1E90FF","#EE6363","#556B2F","#D6D6D6","#D6D6D6")

Now I plot the melted data:

ggplot(R1_Grundwasserneubildung_Rg1Rg2_Monat_mean_druckreif,
       aes(x=Monat,y=value,color=Projektion,group=Projektion)) + 
       geom_line(size=0.8) + 
       xlab("Monat") +
       ylab("Grundwasserneubildung [mm/Monat]") +
       ggtitle("Grundwasserneubildung") +
       theme_bw() +
       scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
                          labels = c("Jan","Feb","Mär","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez")) +
       theme(axis.title=element_text(size=15,vjust = 0.3, face="bold"),
             title=element_text(size=15,vjust = 1.5,face="bold")) +
       scale_colour_manual(values = Projektionen_Farben)   

Sorry, but I haven't got enough reputation to post a pic of the plot. Now I want to fill/shade the space between the Max-line and the Min-line with, lets say, a light grey (alpha=.3). I have tried with geom_ribbon() but haven't found the right way to define x, ymin and ymax as needed. Does someone know a way to fill the space between these two lines?

解决方案

Use your original data frame for the geom_ribbon() and provide columns Min and Max as ymin and ymax.

  + geom_ribbon(data=R1_Grundwasserneubildung_Rg1Rg2_Monat_mean,
                  aes(x=monat,ymin=Min,ymax=Max),
                   inherit.aes=FALSE,alpha=0.3,color="grey30")

这篇关于如何填补/阴影时间线之间的空间,用ggplot绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆