数据:
代码:
Morocco_ObservedClim <- read.csv(file = "Morocco_ObservedClim.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
Morocco_ProjectedClim <- read.csv(file = "Morocco_ProjectedClim.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
# Select the data
obs_annualanom.df <- subset(Morocco_ObservedClim, DataSource %in% c("avg") & DataFormat %in% c("anom") & Timeframe %in% c("annual"))
obs_annualanom.df <- obs_annualanom.df[1:41,]
proj_annualanom.df <- subset(Morocco_ProjectedClim, gcm %in% c("avg","min","max") & DataFormat %in% c("anom") & Timeframe %in% c("annual"))
# Melting the data
obs_annualanom.df <- melt(obs_annualanom.df, id.vars=c("Year","DataSource","DataFormat","Timeframe"))
proj_annualanom.df <- melt(proj_annualanom.df, id.vars=c("Year","sres","gcm","DataFormat","Timeframe"))
# Plots
precip <- ggplot(data=obs_annualanom.df) + geom_line(aes(Year,value),subset=.(variable %in% c("Precip")),size=1.1)
precip <- precip + geom_line(data=proj_annualanom.df, aes(Year,value),colour="brown",size=1.1,subset=.(variable %in% c("Precip") & gcm %in% c("avg") & sres %in% c("20thC")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="tan1",alpha=0.5,subset=.(variable %in% c("Precip") & gcm %in% c("max") & sres %in% c("20thC")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="tan1",alpha=0.5,subset=.(variable %in% c("Precip") & gcm %in% c("min") & sres %in% c("20thC")))
precip <- precip + geom_line(data=proj_annualanom.df, aes(Year,value),colour="red",size=1.1,subset=.(variable %in% c("Precip") & gcm %in% c("avg") & sres %in% c("A2")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="red3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("max") & sres %in% c("A2")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="red3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("min") & sres %in% c("A2")))
precip <- precip + geom_line(data=proj_annualanom.df, aes(Year,value),colour="blue",size=1.1,subset=.(variable %in% c("Precip") & gcm %in% c("avg") & sres %in% c("A1B")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="royalblue3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("max") & sres %in% c("A1B")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="royalblue3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("min") & sres %in% c("A1B")))
precip <- precip + geom_line(data=proj_annualanom.df, aes(Year,value),colour="green4",size=1.1,subset=.(variable %in% c("Precip") & gcm %in% c("avg") & sres %in% c("B1")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="palegreen3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("max") & sres %in% c("B1")))
precip <- precip + geom_area(data=proj_annualanom.df, aes(Year,value),fill="palegreen3",alpha=0.3,subset=.(variable %in% c("Precip") & gcm %in% c("min") & sres %in% c("B1")))
precip <- precip + labs(title="Precipitation",y="Anomalies (mm/year)", x="") + theme_bw()
#precip <- precip + scale_x_discrete(breaks=seq(by=20)) #+ scale_y_continuous(breaks = seq(-50, 80, by=25))
precip <- precip + theme(plot.title=element_text(face="bold", size=rel(2), hjust=0.5, vjust=1.5),
axis.text.x=element_text(color="black", size=rel(2.5), hjust=0.5, vjust=0.5),
axis.text.y=element_text(color="black", size=rel(2.5), hjust=1),
axis.title=element_text(face="bold", color="black", size=rel(1.7), hjust=0.5, vjust=0.2))
temp <- ggplot(data=obs_annualanom.df) + geom_line(aes(Year,value),subset=.(variable %in% c("Temp")),size=1.1)
temp <- temp + geom_line(data=proj_annualanom.df, aes(Year,value),colour="brown",size=1.1,subset=.(variable %in% c("Temp") & gcm %in% c("avg") & sres %in% c("20thC")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="tan1",alpha=0.5,subset=.(variable %in% c("Temp") & gcm %in% c("max") & sres %in% c("20thC")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="tan1",alpha=0.5,subset=.(variable %in% c("Temp") & gcm %in% c("min") & sres %in% c("20thC")))
temp <- temp + geom_line(data=proj_annualanom.df, aes(Year,value),colour="red",size=1.1,subset=.(variable %in% c("Temp") & gcm %in% c("avg") & sres %in% c("A2")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="red3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("max") & sres %in% c("A2")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="red3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("min") & sres %in% c("A2")))
temp <- temp + geom_line(data=proj_annualanom.df, aes(Year,value),colour="blue",size=1.1,subset=.(variable %in% c("Temp") & gcm %in% c("avg") & sres %in% c("A1B")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="royalblue3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("max") & sres %in% c("A1B")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="royalblue3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("min") & sres %in% c("A1B")))
temp <- temp + geom_line(data=proj_annualanom.df, aes(Year,value),colour="green4",size=1.1,subset=.(variable %in% c("Temp") & gcm %in% c("avg") & sres %in% c("B1")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="palegreen3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("max") & sres %in% c("B1")))
temp <- temp + geom_area(data=proj_annualanom.df, aes(Year,value),fill="palegreen3",alpha=0.3,subset=.(variable %in% c("Temp") & gcm %in% c("min") & sres %in% c("B1")))
temp <- temp + labs(title="Temperature",y="Anomalies (degree C)", x="") + theme_bw()
#temp <- temp + scale_x_discrete(breaks=seq(by=20)) #+ scale_y_continuous(breaks = seq(-50, 80, by=25))
temp <- temp + theme(plot.title=element_text(face="bold", size=rel(2), hjust=0.5, vjust=1.5),
axis.text.x=element_text(color="black", size=rel(2.5), hjust=0.5, vjust=0.5),
axis.text.y=element_text(color="black", size=rel(2.5), hjust=1),
axis.title=element_text(face="bold", color="black", size=rel(1.7), hjust=0.5, vjust=0.2))
我的结果:
我的目标:重现此图
图形命名法:
实线代表平均值。
填充区域应该代表数据中可用的最大值和最小值。
我遇到的问题是我想要实现以下目标: 首先,根据我得到的图表绘制成功完成的平均值。 其次,能够在数据中的最小值和最大值之间设置填充,这是我失败的地方。
任何帮助都将不胜感激。
由于
答案 0 :(得分:2)
我认为你过分复杂了。考虑:
df <- subset(
Morocco_ProjectedClim,
DataFormat=="anom" & gcm %in% c("avg", "min", "max") & Timeframe=="annual",
select=c("sres", "Year", "gcm", "Temp")
)
df.cast <- dcast(df, sres + Year ~ gcm)
library(ggplot2)
ggplot(df.cast, aes(x=Year, y=avg)) +
geom_ribbon(aes(ymin=min, ymax=max, fill=sres), alpha=0.4) +
geom_line(aes(color=sres)) +
scale_fill_manual(values=c("tan1", "red3", "royalblue3", "palegreen3")) +
scale_color_manual(values=c("brown", "red", "blue", "green4"))
这只是预测数据集的温度,但应该让您对如何解决问题有所了解。
编辑:这会增加实际值:
ggplot(df.cast, aes(x=Year, y=avg)) +
geom_ribbon(aes(ymin=min, ymax=max, fill=sres), alpha=0.4) +
geom_line(aes(color=sres)) +
geom_line(
data=subset(Morocco_ObservedClim, DataSource == "avg" & DataFormat == "anom" & Timeframe == "annual"),
aes(x=Year, y=Temp, color="Actual")
) +
scale_fill_manual(values=c("tan1", "red3", "royalblue3", "palegreen3")) +
scale_color_manual(values=c(`20thC`="brown", A1B="red", A2="blue", B1="green4", Actual="black"))
虽然注意我没有更新情节。