绘制多条线和误差线

Plot multiple lines and error bars

我正在多个地点使用观察到的和建模的土壤水分测量值:

DF <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L), .Label = c("CA-Oas", "CA-Ojp", "CA-Qfo", 
"US-Ho1", "US-UMB"), class = "factor"), month = c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), satellite = c(NA, 
NA, NA, 0.246855412760089, 0.239430871664309, 0.247785585430952, 
0.254201344766859, 0.228525727473456, 0.220153005451572, 0.248914102315903, 
0.226286688271691, NA, NA, NA, NA, 0.289740440845489, 0.377737456677007, 
0.349513851965849, 0.369372068320291, 0.33528384697019, 0.316710417976185, 
0.353778275847435, 0.366419460285794, NA, NA, NA, NA, NA, 0.523234443318459, 
0.541902482509613, 0.541902482509613, 0.541902482509613, 0.541902482509613, 
0.541902482509613, 0.541902482509613, NA, NA, NA, 0.490694537758827, 
0.592309034864108, 0.636846342572459, 0.645659983158112, 0.642242492328991, 
0.644422933720706, 0.634390437856634, 0.604341197472352, 0.601287194034632, 
0.586221873760223, NA, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923, 0.624189734458923, 
0.624189734458923), satellite.low = c(NA, NA, NA, 0.208491480949955, 
0.208809739158129, 0.190487245023279, 0.206581580485592, 0.190563366897309, 
0.180458581035219, 0.229389992708553, 0.200156716900049, NA, 
NA, NA, NA, 0.197556973794879, 0.303169270710883, 0.255839831380852, 
0.308712828188052, 0.272073699535891, 0.262898007889838, 0.279825783579647, 
0.257326671080677, NA, NA, NA, NA, NA, 0.477209513195344, 0.541902482509613, 
0.541902482509613, 0.541902482509613, 0.541902482509613, 0.541902482509613, 
0.541902482509613, NA, NA, NA, 0.364323639893309, 0.534208357528997, 
0.611485343460275, 0.643659507474862, 0.627827219126354, 0.637549292123253, 
0.608606893522788, 0.5526295760826, 0.539322500377704, 0.519807807424512, 
NA, NA, 0.624189734458923, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923), satellite.high = c(NA, 
NA, NA, 0.285219344570222, 0.270052004170489, 0.305083925838626, 
0.301821109048126, 0.266488088049604, 0.259847429867925, 0.268438211923252, 
0.252416659643333, NA, NA, NA, NA, 0.3819239078961, 0.45230564264313, 
0.443187872550847, 0.430031308452529, 0.398493994404489, 0.370522828062531, 
0.427730768115223, 0.47551224949091, NA, NA, NA, NA, NA, 0.569259373441575, 
0.541902482509613, 0.541902482509613, 0.541902482509613, 0.541902482509613, 
0.541902482509613, 0.541902482509613, NA, NA, NA, 0.617065435624345, 
0.650409712199219, 0.662207341684644, 0.647660458841361, 0.656657765531627, 
0.651296575318159, 0.660173982190479, 0.656052818862104, 0.663251887691561, 
0.652635940095934, NA, NA, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923, 0.624189734458923, 
0.624189734458923, 0.624189734458923, 0.624189734458923, 0.624189734458923
), observed = c(0.140086734851409, 0.137745990685859, 0.146660019201229, 
0.275950971628449, 0.298260250896057, 0.26870029739777, 0.227566661823465, 
0.197824137311287, 0.195409734063355, 0.229745648248465, 0.226546607074933, 
0.158508782420749, 0.0809095246636771, 0.0804010923965351, 0.0845644708882278, 
0.136702248824284, 0.121883242349049, 0.108167424836601, 0.0970784232538687, 
0.0860934461299105, 0.0910916878172589, 0.10747642248062, 0.102700195758564, 
0.0811833903700756, 0.115733715437788, 0.0631616319005478, 0.0631265153446416, 
0.171535848109378, 0.18694684173028, 0.142807562821677, 0.145926108701425, 
0.154393702185792, 0.171436382382201, 0.188897212829005, 0.186402403754978, 
0.165098945598251, 0.0713685071127924, 0.0436531172429078, 0.0624862109235555, 
0.127141665482761, 0.134542260869565, 0.124414092512545, 0.100807230998223, 
0.0765214392215714, 0.0798724029741452, 0.103098854664915, 0.116568256944444, 
0.1105108739241, 0.108650005144474, 0.0976296689160692, 0.105006219572287, 
0.122777662914972, 0.102765292125318, 0.0851933017211099, 0.0566760862577016, 
0.056282148272957, 0.0718264626865672, 0.0909327257326783, 0.10461694624978, 
0.103895834299474), observed.low = c(0.123032811442984, 0.126127332034484, 
0.118118534835286, 0.198817004012519, 0.254140718534211, 0.208009439993492, 
0.164189152182023, 0.138753714747272, 0.124966620815314, 0.161415523471958, 
0.159972232752574, 0.121607685043651, 0.0591772062927612, 0.05892126834644, 
0.0577332139485083, 0.0979872715014624, 0.0939120895219702, 0.0785008888345453, 
0.0635909823809719, 0.0516671874880994, 0.0513847048326183, 0.0716629121839855, 
0.0686069735468453, 0.0605781104970885, 0.0725611238034912, 0.0287878644886994, 
0.0312612891352408, 0.117719361193379, 0.158601178554096, 0.103448327552773, 
0.108434377599704, 0.123534447310382, 0.150300870070247, 0.171762349088762, 
0.173223674947214, 0.143153513926194, 0.0176750483828094, -0.00825194618307156, 
0.00841588326444485, 0.0851767193470053, 0.114292404939469, 0.104180435677072, 
0.0730616681527658, 0.03446185464827, 0.0417092732525248, 0.0672335656317878, 
0.091673056919691, 0.0718925232829272, 0.0765541880959607, 0.0759211571229279, 
0.0803948566686958, 0.0858717319021568, 0.0760499923996711, 0.0506548126690479, 
0.0369099617804679, 0.0337436690922423, 0.0466183548891693, 0.0663011553037621, 
0.0900575679369071, 0.0899421880715561), observed.high = c(0.157140658259833, 
0.149364649337235, 0.175201503567172, 0.353084939244379, 0.342379783257904, 
0.329391154802047, 0.290944171464907, 0.256894559875301, 0.265852847311397, 
0.298075773024972, 0.293120981397293, 0.195409879797847, 0.102641843034593, 
0.10188091644663, 0.111395727827947, 0.175417226147105, 0.149854395176127, 
0.137833960838657, 0.130565864126765, 0.120519704771722, 0.130798670801899, 
0.143289932777255, 0.136793417970284, 0.101788670243063, 0.158906307072085, 
0.0975353993123963, 0.0949917415540424, 0.225352335025378, 0.215292504906464, 
0.182166798090582, 0.183417839803146, 0.185252957061203, 0.192571894694156, 
0.206032076569248, 0.199581132562743, 0.187044377270308, 0.125061965842775, 
0.0955581806688872, 0.116556538582666, 0.169106611618516, 0.154792116799661, 
0.144647749348019, 0.128552793843681, 0.118581023794873, 0.118035532695766, 
0.138964143698041, 0.141463456969198, 0.149129224565273, 0.140745822192987, 
0.11933818070921, 0.129617582475879, 0.159683593927787, 0.129480591850964, 
0.119731790773172, 0.0764422107349353, 0.0788206274536718, 0.097034570483965, 
0.115564296161594, 0.119176324562654, 0.117849480527392)), .Names = c("site", 
"month", "modeled", "modeled.low", "modeled.high", "observed", 
"observed.low", "observed.high"), row.names = c(NA, -60L), class = "data.frame")

我需要做的是创建 "modeled" 和 "observed" 土壤湿度与月份的线图,并添加相应的误差线 modeled.lowmodeled.high observed.lowobserved.high 到那些行。

此外,我需要根据 site 列创建分面。

我的第一个方法是融化该数据框并从那里开始,但误差线可能会使该方法复杂化:

library(reshape2)
library(ggplot2)

DF.m <- melt(DF, id=c('site','month'))

ggplot(data=DF.m) + 
  geom_line(aes(x=month, y=value, colour=variable, group=variable)) +
  facet_wrap(~site) +
  theme_bw(base_size = 18) +
  scale_x_discrete(limits=month.abb) + 
  ylab('Soil water content (%)') + xlab('')

这显然不起作用,因为 modeled.lowmodeled.high 以及 observed.lowobserved.high 被解释为要绘制的线条,而我需要它们作为误差线。

我知道我应该在这段代码中使用 geom_errorbar() 来实现我需要的,但我不确定如何将它与 molten 数据框一起使用。

有什么建议吗?

library(dplyr)
df_m <- DF %>% 
        select(site:modeled.high) %>% 
        mutate(var="modeled") %>% 
        setNames(c("site", "month", "val", "low", "high", "var"))

df_ob <- DF %>% 
         select(site:month, observed:observed.high) %>% 
         mutate(var="observed") %>% 
         setNames(c("site", "month", "val", "low", "high", "var"))

df <- rbind(df_m, df_ob)

ggplot(df, aes(month, val, colour=var)) +
  geom_errorbar(aes(ymax=high, ymin=low)) +
  geom_point() +
  facet_wrap(~site)

带有说明高值和低值范围的带状线图:

ggplot(df, aes(month, val, colour=var)) +
  geom_ribbon(aes(ymax=high, ymin=low, linetype=NA), alpha=.2) +
  geom_line() +
  facet_wrap(~site)

一个data.table解决方案:

library(data.table)
plt <- melt(as.data.table(DF), id=1:2, measure = patterns("modeled$|observed$", "low", "high"))
plt[, variable := factor(variable, levels = 1:2, labels = c('modeled', 'observed'))]

ggplot(plt, aes(x = month, y = value1, ymin = value2, ymax = value3, color = variable)) +
    geom_line() +
    geom_errorbar() +
    facet_wrap(~site)