代碼:
## Load the data
ifpricc = read.csv(file = "IFPRI_CCAgg2050.csv", heade=TRUE)
#-----------------------------------------------------------------------
# Plotting Kernel density distribution for the final yield impact data
#-----------------------------------------------------------------------
ifpricc.df = as.data.frame(ifpricc)
ifpricc_mlt.df = melt(ifpricc.df, id.vars=c("crop","codereg","reg","sres","gcm","scen"))
kernel = ggplot(data=subset(ifpricc_mlt.df, reg %in% c("Canada","United States","Oceania","OECD Europe","Eastern Europe","Former USSR") & gcm %in% c("CSIRO","MIROC","noCC")),
aes(x = value, y = ..density..))
kernel = kernel + geom_density(aes(fill = gcm), alpha=.4, subset = .(crop %in% c("WHET")),
position="identity", stat="density", size=0.75,
bw = "nrd0", adjust = 1.5,
kernel = c("gaussian"))
kernel = kernel + scale_fill_manual(name="GCM model",breaks=c("CSIRO","MIROC","noCC"), values=c("red","blue","gray80"))
kernel = kernel + facet_grid(sres ~ reg, scale="free") + scale_y_continuous(breaks=seq(0,2,.25))
kernel = kernel + labs(title="Kernel density distribution - with and without climate change", y="Density", x="Yield") + theme_bw()
kernel = kernel + theme(plot.title=element_text(face="bold", size=rel(2), hjust=0.5, vjust=1.5, family="serif"),
axis.text.x=element_text(color="black", size=rel(2), hjust=0.5, family="serif"),
axis.text.y=element_text(color="black", size=rel(2), hjust=1, family="serif"),
axis.title.x=element_text(face="bold", color="black", size=rel(1.6), hjust=0.5, vjust=0.2, family="serif"),
axis.title.y=element_text(face="bold", color="black", size=rel(1.6), hjust=0.5, vjust=0.2, family="serif"),
strip.text=element_text(face="bold", size=rel(1.5), family="serif"),
legend.text=element_text(face="bold", size=rel(1.25), family="serif"),
legend.title=element_text(face="bold", size=rel(1.45), family="serif"))
結果:
問:
我想在這裏實現的是繪製內核密度曲線。我的問題是,我想覆蓋基線內核曲線(在較低的方面)在有色的(兩個上方面)和代表偏離基線。任何幫助將不勝感激。
乾杯:)
選擇問:
所以我修修補補的網站上查找可能的解決方案後一點,我想出了這一點:不是通過「SRES」使用facet_grid(sres ~ reg)
刻面x「reg」,我使用facet_wrap(~ reg)
分面。它產生了更接近我想要的東西。
現在的問題是,我無法通過「sres」來識別分配,這正是我所需要的。爲了解決這個問題,我想通過添加用「sres」繪製數據平均值的垂直線來標註該圖。但我有點失去了如何從這裏移動。
有什麼建議嗎?
我懷疑我需要修改的數據結構:)我只是一廂情願地希望我不會要。另一方面,比較facet_grid()和facet_wrap()版本,我認爲後者看起來更好。因此,你能幫我解決另一個問題:我的目標是通過sres來計算平均值嗎? – iouraich
@ smailov83,請參閱編輯 –