在两个数据集中的相同列和行上循环 ggplot/ggvis

Looping ggplot/ggvis over identical columns and rows in two datasets

我对 R 还很陌生,因此不得不问你一个基本问题。

我有两个大型面板数据集(60 个变量,每个变量代表 30 个国家/地区,范围从 1950 年到 2013 年)。 60个变量同名,数据可能不同也可能不同

我的最终目标是创建 60 个网格,每个网格包含 30 个图:每个网格引用 60 个变量之一,并包含每个国家/地区的图。每个图将包含 2 个折线图,一个来自第一个数据框,另一个来自第二个数据框(每个都针对相同的变量)。

我以前在 Stata 中做过这个,使用全局变量和一个简单的循环。我一直在尝试在 R 中完成这项工作。

我现在将数据转换为宽格式(列:日期、国家/地区、Indicator1、...Indicator60),但我读到 ggplot2 在长格式下表现更好(?)。

我的主要问题是如何循环(for, lapply, function..)。 .

如果没有答案,我将非常感谢有关如何解决此问题的想法或提示,以便我在需要时设法提出更具体的问题。

编辑:根据要求在可重现的数据样本下方

year <- c(2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013,2010, 2011, 2012,     
    2013, 2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013, 2010, 2011, 2012,    
    2013, 2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013)
country <- c(rep("Australia", times =8), rep("Canada", times = 8),  
    rep("Australia", times =8), rep("Canada", times = 8))
indicator <- c(rep("Apples", times = 16), rep("Bananas", times = 16))
versiondata <- c(rep("new", times = 4), rep("old", times = 4), rep("new",  
    times = 4), rep("old", times = 4), rep("new", times = 4), rep("old", 
    times = 4), rep("new", times = 4), rep("old", times = 4))
value <- runif(32)
mydf <- data.frame(year, country, indicator, versiondata, value)  

我仍然停留在 do 的确切表达方式上。我想到了这个抱歉的位,我不知道如何指定两个 y 变量(对应于列 versiondata 中的旧和新)。

mydf %>%
  group_by(indicator) %>%
  do({
    p <- ggplot(., aes(x=year)) + 
      geom_line(aes(y = ???)) 
    + facet_wrap(~country) + ggtitle("indicator")
    })

对于这种事情,一个相当标准的方法是:

by(mydf, mydf$indicator, function(X) ggplot(X, aes(year, value, color = versiondata)) + geom_line() + facet_wrap(~country))

使用指标名称作为标题可能需要更多的技巧:

lapply(unique(mydf$indicator), function(X) ggplot(mydf[mydf$indicator == X,], aes(year, value, color = versiondata)) + geom_line() + facet_wrap(~country) + labs(title = X))

每个指标应如下所示: