繁体   English   中英

按多种颜色分组时间序列ggplot2

[英]Group time series by multiple colors ggplot2

鉴于以下数据,我想做以下事情:

  • 用 RCP 为图表着色。
  • RCP45 为blue ,RCP85 为red
  • darkblue的历史是浅蓝色, lightblue的未来是深蓝色
  • RCP85 为red ,RCP85 为red darkred的历史为浅红色, lightred的未来为深红色
  • 两个 RCP 的色带与 RCP 线条颜色相同,但透明度为 60%
  • 删除图中底部的图例

代码

library(ggplot2)
red.bold.italic.text <- element_text(face = "bold", color = "black",size = 15)
blue.bold.italic.16.text <- element_text(face = "plain", color = "black", size = 15)

p=ggplot(datnew1, aes(x=date, y=md, group=interaction(window,rcp),colour=rcp,linetype=rcp)) + 
  geom_line(aes(linetype=rcp,colour=rcp),size=2) + scale_x_discrete(limits = month.abb)+
  geom_ribbon(aes(ymin=lw, ymax=up,fill=rcp), alpha=0.2)+  scale_linetype_manual(values = c(1, 1))+
  theme_light(base_size = 16)+
  scale_colour_manual(values=c("Historical (1981-2010)"= "red","Future (2031-2060)"= "darkred","RCP45"= "blue","RCP85"= "darkblue")) +
  scale_fill_manual(values=c("Historical (1981-2010)"= "red","Future (2031-2060)"= "darkblue","RCP45"= "red","RCP85"= "darkblue"))+
  ylab("Monthly mean precipitation (mm/month)") + xlab("Month")+
  scale_y_continuous(limits=c(10,100),breaks = seq(10,100, by = 10))+
  theme(legend.title = element_text(colour="black", size=18, face="plain"))+
  theme(legend.text = element_text(colour="black", size = 18, face = "plain"))+
  theme(title = red.bold.italic.text, axis.title = red.bold.italic.text)+
  theme(strip.text.x = element_text(size=16, color="black",face="bold"))+
  theme(axis.text = blue.bold.italic.16.text)+
  theme(axis.text.x  = element_text(angle=0, vjust=0, size=14))+
  theme(legend.title = element_blank())+
  theme(legend.position = c(.3, 0.9))
      

样本数据:

datnew1=structure(list(lw = c(16.6753187200483, 21.4289544716693, 29.131024749619, 
            26.4451050660275, 25.5363745874864, 22.3100254118503, 25.3858241712341, 
            21.7361488263869, 21.134925285572, 20.3804263763188, 20.767810433526, 
            15.5624305770072, 13.6599576894344, 19.5870286217936, 29.2201276699968, 
            24.3653328935277, 23.9635547375874, 24.3648836173464, 25.284792696032, 
            24.0981382027895, 26.3242313035245, 19.7226574098932, 20.6275742530069, 
            18.679561555551, 16.8850809156325, 21.2416485017613, 29.7091254906665, 
            29.3512968472398, 26.7643800404397, 20.6036945367284, 24.5774350456584, 
            22.4589202159837, 22.1557012062663, 20.495616391923, 22.62870740558, 
            16.7107200127046, 16.170426743838, 16.0073144937242, 24.8346261566947, 
            27.2487638452208, 25.6803560527223, 21.9109019124414, 24.9863865086006, 
            24.7383114312737, 22.7988626485333, 23.1664598063168, 22.2850514748318, 
            19.9421651968762), md = c(34.1494507550909, 39.6340574750366, 
            58.9372420261637, 56.6207577019454, 43.361798347495, 31.160482158852, 
            38.160386009545, 37.2132524830954, 30.2698620626717, 28.6356804499872, 
            35.233957987861, 29.1865515841786, 29.9445633591394, 37.9819838944512, 
            46.1787164455896, 56.5809298666502, 40.1202806266253, 29.0590366535294, 
            36.2976395078549, 39.7380220888118, 31.893227328217, 28.582972070917, 
            34.5502887204816, 32.7807120331008, 33.3831846326674, 40.2849478591906, 
            59.1880289139864, 56.7037088960098, 42.8604076287803, 30.3220751690578, 
            40.0633676077262, 41.538719566391, 32.3030933476488, 31.174379420567, 
            36.3425256213667, 30.9026792967595, 36.9449594243179, 40.43510680284, 
            53.9008977609403, 58.8220983049378, 37.7929859091667, 32.1785521914888, 
            38.8379251496664, 38.9562538569509, 35.292018146711, 30.803289454849, 
            39.3452540375587, 31.1465430532526), up = c(49.4900589382251, 
            63.5308951899186, 91.531785108199, 78.220911702462, 56.2477741355009, 
            40.9353781062984, 68.0776004538801, 91.4958221642131, 69.7808568218521, 
            50.3736834930193, 47.2715591312579, 57.7095464038687, 46.5845257776357, 
            70.1228590164935, 95.0914019473576, 72.118497958154, 62.6482433527037, 
            42.779379910669, 67.4146286812892, 91.4721043772777, 60.0164597044734, 
            49.4663677312632, 54.2095266493516, 53.1175224882724, 50.5955437662951, 
            62.8873202520482, 93.587594066318, 82.2290200772622, 56.2873793191659, 
            36.9343197371923, 68.9496964848163, 85.3278067646245, 71.9473407588177, 
            48.3634645496618, 45.9028396563072, 60.1620895443098, 49.1215460984992, 
            58.8340089921874, 91.8655959587155, 77.7284443409707, 62.1346455191459, 
            44.8159579590911, 73.4015944646097, 96.9647052135439, 72.7472272047245, 
            55.1406035674027, 46.9831817731707, 60.0913517547588), date = c("Jan", 
            "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
            "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
            "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", 
            "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", 
            "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
            "Nov", "Dec"), window = c("Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Historical (1981-2010)", "Historical (1981-2010)", 
            "Historical (1981-2010)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)", "Future (2031-2060)", "Future (2031-2060)", 
            "Future (2031-2060)"), rcp = c("RCP45", "RCP45", "RCP45", "RCP45", 
            "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", 
            "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", 
            "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP45", "RCP85", 
            "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", 
            "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", 
            "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", "RCP85", 
            "RCP85", "RCP85")), row.names = c(NA, -48L), class = "data.frame")

在此处输入图像描述

像这样?

# I prefer to define groups manually
datnew1$grp <- paste(datnew1$rcp, gsub("([a-zA-Z]*).*", "\\1", datnew1$window))

# Plot
ggplot(datnew1, aes(x=date, y=md, group=grp, color=grp)) +
  geom_ribbon(aes(ymin=lw, ymax=up, fill=grp), alpha=0.2, color=NA) +
  geom_line(size=1) +
  scale_x_discrete(limits=month.abb)+
  scale_colour_manual(values=c("RCP45 Historical" = "red",
                               "RCP45 Future"= "darkred",
                               "RCP85 Historical" = "blue",
                               "RCP85 Future" = "darkblue")) +
  scale_fill_manual(values=c("RCP45 Historical" = "red",
                               "RCP45 Future"= "darkred",
                               "RCP85 Historical" = "blue",
                               "RCP85 Future" = "darkblue")) +
  ylab("Monthly mean precipitation (mm/month)") +
  xlab("Month") +
  scale_y_continuous(limits=c(10,100),breaks = seq(10,100, by = 10)) +
  theme_light(base_size = 16) +
  theme(legend.position = "none")

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM