如何在ggplot中的同一图中叠加多层数据?

时间:2014-03-24 14:23:11

标签: r ggplot2

数据:Data

代码:

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

问题:

我在这里想要实现的是绘制核密度曲线。我的问题是我希望将基线内核曲线(在下方面)覆盖在有色的(两个上方)上,并且表示与基线的偏差。任何帮助将不胜感激。

干杯:)

备选问题:

所以我在网站上查找了潜在的解决方案后稍微修改了一下,我提出了这个问题:而不是使用facet_grid(sres ~ reg)进行分析,而不是使用#34; sres" x" reg",我使用facet_wrap(~ reg)分面。它产生的东西更接近我想要的Alt_plot

现在的问题是我无法通过" sres"识别分发,这正是我要找的。为了解决这个问题,我想通过添加垂直线来注释该图,该线绘制了数据的平均值" sres"。但我有点失去了如何离开这里。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

如果我理解,我认为这就是你想要的。您需要重新排列数据:重复包含noCC因子的数据框中的行,一次使用sres = A1B,一次使用sres = B1。这样,noCC密度曲线将出现在A1B面和B1面中。

此外,除了创建1s的列之外,数据帧的融化没有任何影响。另外,我在调用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