add one mean trend line for different lines in one plot

2.7k views Asked by At

i'm trying to add a trend line (linear mean) to a plot. I want to have one mean trend line for 11 seperate lines in one plot. My plot looks like that:

enter image description here

Now i want to have one (1) mean trend for the coloured lines . The two grey lines are max and min values, that shell not be considered when forming the mean trend line. Only trend line for the coloured lines. My data looks like that:

jahr    beob    werex_00    werex_11    werex_22    werex_33    werex_44    werex_55    werex_66    werex_77    werex_88    werex_99    Min Max 
1961    NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  -18,6304    61,1809 
1962    NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  -11,9439    87,5546 
1963    NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  -15,5266    82,2700 
1964    NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  -40,0319    85,2906 
1965    NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  -34,3038    94,9024 
1966    -3,8771 8,3257  24,8513 13,5184 21,0853 15,6732 1,8496  -8,4903 2,1629  8,2024  16,1356 -31,3819    92,7969 
1967    -9,4982 11,3976 23,0892 13,9196 16,8335 17,2100 2,9570  -10,1167    3,9671  7,2876  10,6471 -7,5067 110,6196    
1968    -11,2913    11,4993 18,3050 8,0152  13,9073 16,8016 4,7250  -12,9246    2,6713  5,4357  3,5107  -51,2107    138,7448    
1969    2,6362  9,1087  19,9564 6,0345  3,8747  11,6780 2,5116  -13,4089    4,3353  0,3998  3,1798  -30,3322    42,1300 
1970    6,6531  2,0238  20,5422 2,3972  0,4017  11,1036 5,0465  -10,2958    8,3485  -2,1658 4,6128  -11,5068    44,3986 
1971    -1,7923 3,0214  18,7542 2,5401  -2,5444 8,3054  1,8535  -10,1233    8,3663  2,0204  1,9454  -22,3762    95,3033 
1972    -1,1383 0,1923  21,6838 -1,2288 -4,1658 4,4113  2,5008  -7,6908 9,6488  2,3338  0,7093  -29,0978    31,1931 
1973    -3,9827 2,7288  19,6430 -0,1119 -3,9149 3,1060  -3,5426 -5,1332 3,9206  1,2295  -6,9931 -30,3709    44,2175 
1974    -5,6800 -0,2600 13,2311 1,2855  -11,7234    -8,2399 -5,7620 -4,3448 8,3248  -0,5500 -5,6376 -44,4719    143,3977    
1975    -0,6020 4,4594  13,4312 1,8103  -11,7964    -11,2020    -2,9454 -0,8192 6,8733  -1,6069 -7,5460 -27,5719    46,5896 
1976    9,0707  4,0255  12,8539 0,5613  -9,0911 -14,6242    -2,0957 0,3028  4,2881  -4,2491 -1,5111 -36,0912    22,2878 
1977    6,2822  7,5568  3,2908  1,3875  -9,0588 -14,0812    -2,3505 2,0262  2,7041  -1,6706 2,6180  -13,8766    99,9908 
1978    8,9476  11,4753 -0,1546 0,4597  -7,2769 -9,6268 -0,7031 1,2291  -0,2699 -2,7092 3,2059  -19,4729    79,3319 
1979    8,9790  6,8742  -3,7599 -1,0170 -4,2174 -11,7519    -2,6872 4,4382  0,2518  -3,8938 4,6531  -39,0920    78,4498 
1980    -0,7982 5,3090  -7,1990 0,3482  0,7074  -3,8802 -1,6347 5,7365  -2,5424 -4,5144 4,2867  -43,8048    95,6593 

.....

now i plottet the date after using melt() for ggplot. Melting transforms data to:

Jahr    Projektion  value   
1961    beob    NA  
1962    beob    NA  
1963    beob    NA  
1964    beob    NA  
1965    beob    NA  
1966    beob    -3,877072807    
1967    beob    -9,498158353    
1968    beob    -11,29127163    
1969    beob    2,636225556 
…   …   …   
1961    werex_00    NA  
1962    werex_00    NA  
1963    werex_00    NA  
1964    werex_00    NA  
1965    werex_00    NA  
1966    werex_00    8,32566516  
1967    werex_00    11,39762871 
1968    werex_00    11,49932052 
1969    werex_00    9,108708251 
…   …   …

Now i plotted the data (pic above):

Projektionen_Farben<-c("#000000","#00EEEE","#EEAD0E","#006400","#BDB76B","#EE7600","#68228B","#8B0000","#1E90FF","#EE6363","#556B2F","#D6D6D6","#D6D6D6")
GJm11_Grundwasserneubildung_Jahr_norm_druckreif <- melt(GJm11_Grundwasserneubildung_Jahr_norm,na.rm = FALSE, id.vars="jahr")
names(GJm11_Grundwasserneubildung_Jahr_norm_druckreif)<-c("Jahr","Projektion","value")
GJm11_Grundwasserneubildung_Jahr_norm_druckreif$Jahr <- as.numeric(factor(Jahr)) 
ggplot(GJm11_Grundwasserneubildung_Jahr_norm_druckreif,aes(x=Jahr,y=value,color=Projektion,group=Projektion))+
  geom_line(size=0.8) + xlab("Jahr") + ylab("Grundwasserneubildung [mm/a]") +
  ggtitle("Grundwasserneubildung") +  theme_bw() +
  scale_x_continuous(breaks=c(1961,1980,2000,2020,2040,2060,2080,2100)) + 
  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) + coord_cartesian(xlim = c(1961, 2100)) + 
  geom_ribbon(data=GJm11_Grundwasserneubildung_Jahr_norm,aes(x=jahr,ymin=Min,ymax=Max),inherit.aes=FALSE,alpha=0.1,color="grey30")

So how can i get the mean trend line for the coloured lines? Also i wolud like to know, if there is a way to add mean trend lines for certain time periods like 1961-2000, 2021-2050 and 2071-2100 as well, just to compare trend for this time periods?

Hope somebody can give me a hint.

2

There are 2 answers

6
Beasterfield On BEST ANSWER

If you want to add a trend line I would consider to not plot them on top of the actual data, because you might not be able to see it. Instead, I would just add a geom_smooth:

 + stat_smooth( aes( y = value, x = jahr), inherit.aes = FALSE )

If you want to color different time periods you could do something like:

GJm11_Grundwasserneubildung_Jahr_norm_druckreif$decade <- as.factor( trunc((GJm11_Grundwasserneubildung_Jahr_norm_druckreif$Jahr -1900) / 10, digits=0) )

and add

+ stat_smooth( aes( y = value, x = jahr, color= decade), inherit.aes = FALSE )

instead to your plot.

enter image description here

1
agstudy On

you can use stat_summary

last_plot() + stat_summary(fun.y=mean, geom="line")