2014-03-24 96 views
3

數據:Data如何覆蓋ggplot中同一圖中的多個數據層?

代碼:

## 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")) 

結果:Plot

問:

我想在這裏實現的是繪製內核密度曲線。我的問題是,我想覆蓋基線內核曲線(在較低的方面)在有色的(兩個上方面)和代表偏離基線。任何幫助將不勝感激。

乾杯:)

選擇問:

所以我修修補補的網站上查找可能的解決方案後一點,我想出了這一點:不是通過「SRES」使用facet_grid(sres ~ reg)刻面x「reg」,我使用facet_wrap(~ reg)分面。它產生了更接近我想要的東西Alt_plot

現在的問題是,我無法通過「sres」來識別分配,這正是我所需要的。爲了解決這個問題,我想通過添加用「sres」繪製數據平均值的垂直線來標註該圖。但我有點失去了如何從這裏移動。

有什麼建議嗎?

回答

2

如果我明白了,我認爲這是你想要的。您需要重新排列數據:重複數據幀中包含noCC因子的行,使用sres = A1B一次,並使用sres = B1一次。這樣,noCC密度曲線將出現在A1B構面和B1構面中。

另外,數據幀的融化除了創建一列1之外沒有其他影響。另外,我在ggplot2之外調用子集。

library(ggplot2) 
ifpricc = read.csv(file = "IFPRI_CCAgg2050.csv", heade=TRUE) 

# Subset the data frame 
df = subset(ifpricc, 
    reg %in% c("Canada","United States","Oceania","OECD Europe","Eastern Europe","Former USSR") & 
    gcm %in% c("CSIRO","MIROC","noCC") & 
    crop %in% c("WHET")) 

# Manipulate the data frame 
x = df[df$sres == "PM", ] 
x = rbind(x, x) 
x$sres = rep(c("A1B", "B1"), each = dim(x)[1]/2) 
df = df[df$sres != "PM",] 
df = rbind(df, x) 

# Draw the plot 
ggplot(data=df, aes(x = yield, fill = gcm)) + 
    geom_density(alpha=.4, size=0.75, adjust = 1.5) + 
    scale_fill_manual(name="GCM model",breaks=c("CSIRO","MIROC","noCC"), 
    values=c("red","blue","gray80")) + 
    facet_grid(sres ~ reg, scale="free") + scale_y_continuous(breaks=seq(0,2,.25)) 

enter image description here

編輯:facet_wrap版本: 的想法是繪製兩個圖表:一個用於A1B,並且所述第二對B1;然後使用gridExtra包中的函數將這兩個圖表放在一起。但是,這將爲每個圖表提供一個圖例。只有一個圖例會更好看。因此,繪製其中一個圖表,以便可以提取其圖例。然後繪製兩個沒有他們的傳說的圖表,並將兩個圖表和圖例放在一起。

library(ggplot) 
library(gridExtra) 
library(gtable) 
ifpricc = read.csv(file = "IFPRI_CCAgg2050.csv", heade=TRUE) 

# Subset the data frame 
df = subset(ifpricc, 
    reg %in% c("Canada","United States","Oceania","OECD Europe","Eastern Europe","Former USSR") & 
    gcm %in% c("CSIRO","MIROC","noCC") & 
    crop %in% c("WHET")) 

# Draw first chart 
p1 = ggplot(data=df[df$sres != "B1", ], aes(x = yield, fill = gcm)) + 
    geom_density(alpha=.4, size=0.75, adjust = 1.5) + 
    ggtitle("sres = A1B") + 
    scale_fill_manual(name="GCM model",breaks=c("CSIRO","MIROC","noCC"), 
    values=c("red","blue","gray80")) + 
    facet_wrap(~ reg, scales = "free_x") + scale_y_continuous(breaks=seq(0,2,.25)) 

# Extract its legend 
legend = gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box") 

# Redraw the first chart without its legend 
p1 = p1 + guides(fill = FALSE) 

# Draw the second chart without its legend 
p2 = ggplot(data=df[df$sres != "A1B", ], aes(x = yield, fill = gcm)) + 
    geom_density(alpha=.4, size=0.75, adjust = 1.5) + 
    ggtitle("sres = B1") + 
    scale_fill_manual(name="GCM model",breaks=c("CSIRO","MIROC","noCC"), 
    values=c("red","blue","gray80"), guide = "none") + 
    facet_wrap(~ reg, scales = "free_x") + scale_y_continuous(breaks=seq(0,2,.25)) 

# Combine the two charts and the legend (and a main title) 
grid.arrange(arrangeGrob(p1, p2, ncol = 1), 
    legend, widths = unit.c(unit(1, "npc") - legend$width, legend$width), nrow = 1, 
    main = textGrob("Kernel density distribution - with and without climate change", 
    vjust = 1, gp = gpar(fontface = "bold"))) 

enter image description here

+0

我懷疑我需要修改的數據結構:)我只是一廂情願地希望我不會要。另一方面,比較facet_grid()和facet_wrap()版本,我認爲後者看起來更好。因此,你能幫我解決另一個問題:我的目標是通過sres來計算平均值嗎? – iouraich

+1

@ smailov83,請參閱編輯 –