1

我正在使用 rmaps [ https://github.com/ramnathv/rMaps/blob/master/R/Datamaps.R#L43]中的 ichoropleth 函数来构建动画 choropleth。我想按月而不是按年制作动画。为了实现这一点,我已将代码中所有年份的实例更改为月份。显示第一个月的数据,但不会播放动画。如果我的代码更改正确,我怀疑问题可能与月份有关,但我无法在保留正确格式的同时将其转换为数字或日期。任何人都可以提供解决方案吗?我的数据样本如下

structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2013-03", 
"2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", 
"2013-10", "2013-11", "2013-12", "2014-01", "2014-02", "2014-03", 
"2014-04", "2014-05", "2014-06", "2014-07", "2014-08"), class = "factor"), 
iso = structure(c(2L, 5L, 6L, 7L, 8L, 15L), .Label = c("ABW", 
"AFG", "AGO", "AIA", "ALB", "ARE", "ARG", "AUS", "AUT", "AZE", 
"BEL", "BEN", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLR", 
"BLZ", "BMU", "BRA", "BRB", "BWA", "CAF", "CAN", "CHE", "CHL", 
"CHN", "CMR", "COL", "COM", "CRI", "CUB", "CYM", "CYP", "CZE", 
"DEU", "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", 
"ESP", "EST", "ETH", "FIN", "FJI", "FRA", "FRO", "GAB", "GBR", 
"GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNQ", "GRC", 
"GRD", "GTM", "GUF", "GUM", "GUY", "HKG", "HND", "HRV", "HTI", 
"HUN", "IDN", "IMN", "IND", "IRL", "IRQ", "ISL", "ISR", "ITA", 
"JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KWT", 
"LBN", "LBR", "LBY", "LCA", "LKA", "LSO", "LTU", "LUX", "LVA", 
"MAR", "MCO", "MDV", "MEX", "MLI", "MLT", "MMR", "MNG", "MOZ", 
"MRT", "MUS", "MWI", "MYS", "NAM", "NCL", "NER", "NGA", "NIC", 
"NLD", "NOR", "NPL", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", 
"PNG", "POL", "PRI", "PRT", "PRY", "QAT", "ROU", "RWA", "SAU", 
"SDN", "SEN", "SGP", "SLB", "SLE", "SLV", "SOM", "SRB", "SSD", 
"SUR", "SVK", "SVN", "SWE", "SYC", "TGO", "THA", "TJK", "TKM", 
"TON", "TTO", "TUN", "TUR", "UGA", "UKR", "URY", "USA", "UZB", 
"VNM", "VUT", "WSM", "YEM", "ZAF", "ZMB", "ZWE"), class = "factor"), 
volume = c(1L, 1L, 5L, 4L, 12L, 10L)), .Names = c("month", 
"iso", "volume"), row.names = c(NA, 6L), class = "data.frame")

代码:

Mchoropleth <- function(x, data, pal = "Blues", ncuts = 5, animate = NULL, play = F, map = 
 'world', legend = TRUE, labels = TRUE, ...){
d <- Datamaps$new()
fml = lattice::latticeParseFormula(x, data = data)
data = transform(data, 
fillKey = cut(
  fml$left, 
  unique(quantile(fml$left, seq(0, 1, 1/ncuts))),
  ordered_result = TRUE
 )
)
fillColors = RColorBrewer::brewer.pal(ncuts, pal)
d$set(
scope = map, 
fills = as.list(setNames(fillColors, levels(data$fillKey))), 
legend = legend,
labels = labels,
...
)
if (!is.null(animate)){
range_ = summary(data[[animate]])
data = dlply(data, animate, function(x){
  y = toJSONArray2(x, json = F)
  names(y) = lapply(y, '[[', fml$right.name)
  return(y)
 })
 d$set(
  bodyattrs = "ng-app ng-controller='rChartsCtrl'"  
)
d$addAssets(
  jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
if (play == T){
  d$setTemplate(chartDiv = sprintf("
    <div class='container'>
     <button ng-click='animateMap()'>Play</button>
     <div id='{{chartId}}' class='rChart datamaps'></div>  
    </div>
    <script>
      function rChartsCtrl($scope, $timeout){
        $scope.month = %s;
          $scope.animateMap = function(){
          if ($scope.month > %s){
            return;
          }
          map{{chartId}}.updateChoropleth(chartParams.newData[$scope.month]);
          $scope.month += 1
          $timeout($scope.animateMap, 1000)
        }
      }
   </script>", range_[1], range_[6])
  )

} else {
  d$setTemplate(chartDiv = sprintf("
    <div class='container'>
      <input id='slider' type='range' min=%s max=%s ng-model='value' width=200>
      <div id='{{chartId}}' class='rChart datamaps'></div>  
    </div>
    <script>
      function rChartsCtrl($scope){
        $scope.month = %s;
        $scope.$watch('month', function(newMonth){
          map{{chartId}}.updateChoropleth(chartParams.newData[newMonth]);
        })
      }
   </script>", range_[1], range_[6], range_[1])
  )
}
d$set(newData = data, data = data[[1]])

} else {
d$set(data = dlply(data, fml$right.name))
}
return(d)
}
4

1 回答 1

4

我将尝试制作一个完全可重现的代码示例,包括您上面问题中的一些内容。

首先,设置您提供的数据。

dt <- structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2013-03", 
"2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", 
"2013-10", "2013-11", "2013-12", "2014-01", "2014-02", "2014-03", 
"2014-04", "2014-05", "2014-06", "2014-07", "2014-08"), class = "factor"), 
iso = structure(c(2L, 5L, 6L, 7L, 8L, 15L), .Label = c("ABW", 
"AFG", "AGO", "AIA", "ALB", "ARE", "ARG", "AUS", "AUT", "AZE", 
"BEL", "BEN", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLR", 
"BLZ", "BMU", "BRA", "BRB", "BWA", "CAF", "CAN", "CHE", "CHL", 
"CHN", "CMR", "COL", "COM", "CRI", "CUB", "CYM", "CYP", "CZE", 
"DEU", "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", 
"ESP", "EST", "ETH", "FIN", "FJI", "FRA", "FRO", "GAB", "GBR", 
"GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNQ", "GRC", 
"GRD", "GTM", "GUF", "GUM", "GUY", "HKG", "HND", "HRV", "HTI", 
"HUN", "IDN", "IMN", "IND", "IRL", "IRQ", "ISL", "ISR", "ITA", 
"JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KWT", 
"LBN", "LBR", "LBY", "LCA", "LKA", "LSO", "LTU", "LUX", "LVA", 
"MAR", "MCO", "MDV", "MEX", "MLI", "MLT", "MMR", "MNG", "MOZ", 
"MRT", "MUS", "MWI", "MYS", "NAM", "NCL", "NER", "NGA", "NIC", 
"NLD", "NOR", "NPL", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", 
"PNG", "POL", "PRI", "PRT", "PRY", "QAT", "ROU", "RWA", "SAU", 
"SDN", "SEN", "SGP", "SLB", "SLE", "SLV", "SOM", "SRB", "SSD", 
"SUR", "SVK", "SVN", "SWE", "SYC", "TGO", "THA", "TJK", "TKM", 
"TON", "TTO", "TUN", "TUR", "UGA", "UKR", "URY", "USA", "UZB", 
"VNM", "VUT", "WSM", "YEM", "ZAF", "ZMB", "ZWE"), class = "factor"), 
volume = c(1L, 1L, 5L, 4L, 12L, 10L)), .Names = c("month", 
"iso", "volume"), row.names = c(NA, 6L), class = "data.frame")

该数据虽然只包含 6 行,但都是同一月份,所以我使用您提供的级别iso(ISO 国家代码)和month. 我就叫吧dt2。为了将来参考,提供有用的数据非常有帮助。

  dt2 <- data.frame(
    iso = as.factor(rep(levels(dt$iso),length(levels(dt$month))))
    ,month = unlist(lapply(1:length(levels(dt$month)),function(m){
      rep(levels(dt$month)[m],length(levels(dt$iso)))
    }))
    ,volume = runif(length(levels(dt$month))*length(levels(dt$iso)),0,100)
  )

如果您需要factors让我知道,但通常在使用and or时将因子转换为numericor值通常是明智的。characterrChartsrMapsJSON

  # no reason to have factors
  # so I suggest converting to character
  dt2$iso <- as.character(dt2$iso)
  dt2$month <- as.character(dt2$month)

您是正确的,因为问题是由于使用因素造成的,但更具体地说,该ichorolpleth函数需要数字而不是字符。有多种方法可以解决这些问题。我选择了这条路线

  Mchoropleth <- function(x, data, pal = "Blues", ncuts = 5, animate = NULL, play = F, map = 
   'world', legend = TRUE, labels = TRUE, ...){
    d <- Datamaps$new()
    fml = lattice::latticeParseFormula(x, data = data)
    data = transform(data, 
    fillKey = cut(
      fml$left, 
      unique(quantile(fml$left, seq(0, 1, 1/ncuts))),
      ordered_result = TRUE
     )
    )
    fillColors = RColorBrewer::brewer.pal(ncuts, pal)
    d$set(
    scope = map, 
    fills = as.list(setNames(fillColors, levels(data$fillKey))), 
    legend = legend,
    labels = labels,
    ...
    )
    if (!is.null(animate)){

    range_ = sort(unique(data[[animate]]))


    data = dlply(data, animate, function(x){
      y = toJSONArray2(x, json = F)
      names(y) = lapply(y, '[[', fml$right.name)
      return(y)
     })
     d$set(
      bodyattrs = "ng-app ng-controller='rChartsCtrl'"  
    )
    d$addAssets(
      jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
    )
    if (play == T){
      d$setTemplate(chartDiv = sprintf("
        <div class='container'>
         <button ng-click='animateMap()'>Play</button>
         <div id='{{chartId}}' class='rChart datamaps'></div>  
        </div>
        <script>
          function rChartsCtrl($scope, $timeout){
            $scope.keynum = %s;
              $scope.animateMap = function(){
              if ($scope.keynum === Object.keys(chartParams.newData).length){
                return;
              }
              map{{chartId}}.updateChoropleth(chartParams.newData[Object.keys(chartParams.newData)[$scope.keynum]]);
              $scope.keynum += 1
              $timeout($scope.animateMap, 1000)
            }
          }
       </script>", 0  )
      )

    } else {
      d$setTemplate(chartDiv = sprintf("
        <div class='container'>
          <input id='slider' type='range' min=%s max=%s ng-model='value' width=200>
          <div id='{{chartId}}' class='rChart datamaps'></div>  
        </div>
        <script>
          function rChartsCtrl($scope){
            $scope.month = %s;
            $scope.$watch('month', function(newMonth){
              map{{chartId}}.updateChoropleth(chartParams.newData[newMonth]);
            })
          }
       </script>", range_[1], range_[6], range_[1])
      )
    }
    d$set(newData = data, data = data[[1]])

    } else {
    d$set(data = dlply(data, fml$right.name))
    }
    return(d)
  }

为了隔离重要的部分,我将其粘贴在下面,以便我可以通过它进行讨论。 range_使用了对字符不起作用的摘要,所以我将其更改为

    range_ = sort(unique(data[[animate]]))

我们实际上可以消除这一点,但这是另一个话题。然后$scope.month += 1将无法工作,因为我们使用的是字符,所以我用索引遍历数据的键。我们从$scope.keynum = %s设置为 0 开始,然后添加 1$scope.keynum += 1直到我们到达 end $scope.keynum === Object.keys(chartParams.newData).length

      d$setTemplate(chartDiv = sprintf("
        <div class='container'>
         <button ng-click='animateMap()'>Play</button>
         <div id='{{chartId}}' class='rChart datamaps'></div>  
        </div>
        <script>
          function rChartsCtrl($scope, $timeout){
            $scope.keynum = %s;
              $scope.animateMap = function(){
              if ($scope.keynum === Object.keys(chartParams.newData).length){
                return;
              }
              map{{chartId}}.updateChoropleth(chartParams.newData[Object.keys(chartParams.newData)[$scope.keynum]]);
              $scope.keynum += 1
              $timeout($scope.animateMap, 1000)
            }
          }
       </script>", 0  )
      )

这些 R+Javascipt+Angular 可能很难调试,所以我希望这会有所帮助。我假设您看到这篇文章解释了正在发生的一些事情,但如果您没有看到,我会发布。

这是整个可重现的代码。

library(rCharts)
library(rMaps)
library(plyr)

dt <- structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2013-03", 
"2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", 
"2013-10", "2013-11", "2013-12", "2014-01", "2014-02", "2014-03", 
"2014-04", "2014-05", "2014-06", "2014-07", "2014-08"), class = "factor"), 
iso = structure(c(2L, 5L, 6L, 7L, 8L, 15L), .Label = c("ABW", 
"AFG", "AGO", "AIA", "ALB", "ARE", "ARG", "AUS", "AUT", "AZE", 
"BEL", "BEN", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLR", 
"BLZ", "BMU", "BRA", "BRB", "BWA", "CAF", "CAN", "CHE", "CHL", 
"CHN", "CMR", "COL", "COM", "CRI", "CUB", "CYM", "CYP", "CZE", 
"DEU", "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", 
"ESP", "EST", "ETH", "FIN", "FJI", "FRA", "FRO", "GAB", "GBR", 
"GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNQ", "GRC", 
"GRD", "GTM", "GUF", "GUM", "GUY", "HKG", "HND", "HRV", "HTI", 
"HUN", "IDN", "IMN", "IND", "IRL", "IRQ", "ISL", "ISR", "ITA", 
"JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KWT", 
"LBN", "LBR", "LBY", "LCA", "LKA", "LSO", "LTU", "LUX", "LVA", 
"MAR", "MCO", "MDV", "MEX", "MLI", "MLT", "MMR", "MNG", "MOZ", 
"MRT", "MUS", "MWI", "MYS", "NAM", "NCL", "NER", "NGA", "NIC", 
"NLD", "NOR", "NPL", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", 
"PNG", "POL", "PRI", "PRT", "PRY", "QAT", "ROU", "RWA", "SAU", 
"SDN", "SEN", "SGP", "SLB", "SLE", "SLV", "SOM", "SRB", "SSD", 
"SUR", "SVK", "SVN", "SWE", "SYC", "TGO", "THA", "TJK", "TKM", 
"TON", "TTO", "TUN", "TUR", "UGA", "UKR", "URY", "USA", "UZB", 
"VNM", "VUT", "WSM", "YEM", "ZAF", "ZMB", "ZWE"), class = "factor"), 
volume = c(1L, 1L, 5L, 4L, 12L, 10L)), .Names = c("month", 
"iso", "volume"), row.names = c(NA, 6L), class = "data.frame")


  Mchoropleth <- function(x, data, pal = "Blues", ncuts = 5, animate = NULL, play = F, map = 
   'world', legend = TRUE, labels = TRUE, ...){
    d <- Datamaps$new()
    fml = lattice::latticeParseFormula(x, data = data)
    data = transform(data, 
    fillKey = cut(
      fml$left, 
      unique(quantile(fml$left, seq(0, 1, 1/ncuts))),
      ordered_result = TRUE
     )
    )
    fillColors = RColorBrewer::brewer.pal(ncuts, pal)
    d$set(
    scope = map, 
    fills = as.list(setNames(fillColors, levels(data$fillKey))), 
    legend = legend,
    labels = labels,
    ...
    )
    if (!is.null(animate)){

    range_ = sort(unique(data[[animate]]))


    data = dlply(data, animate, function(x){
      y = toJSONArray2(x, json = F)
      names(y) = lapply(y, '[[', fml$right.name)
      return(y)
     })
     d$set(
      bodyattrs = "ng-app ng-controller='rChartsCtrl'"  
    )
    d$addAssets(
      jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
    )
    if (play == T){
      d$setTemplate(chartDiv = sprintf("
        <div class='container'>
         <button ng-click='animateMap()'>Play</button>
         <div id='{{chartId}}' class='rChart datamaps'></div>  
        </div>
        <script>
          function rChartsCtrl($scope, $timeout){
            $scope.keynum = %s;
              $scope.animateMap = function(){
              if ($scope.keynum === Object.keys(chartParams.newData).length){
                return;
              }
              map{{chartId}}.updateChoropleth(chartParams.newData[Object.keys(chartParams.newData)[$scope.keynum]]);
              $scope.keynum += 1
              $timeout($scope.animateMap, 1000)
            }
          }
       </script>", 0  )
      )

    } else {
      d$setTemplate(chartDiv = sprintf("
        <div class='container'>
          <input id='slider' type='range' min=%s max=%s ng-model='value' width=200>
          <div id='{{chartId}}' class='rChart datamaps'></div>  
        </div>
        <script>
          function rChartsCtrl($scope){
            $scope.month = %s;
            $scope.$watch('month', function(newMonth){
              map{{chartId}}.updateChoropleth(chartParams.newData[newMonth]);
            })
          }
       </script>", range_[1], range_[6], range_[1])
      )
    }
    d$set(newData = data, data = data[[1]])

    } else {
    d$set(data = dlply(data, fml$right.name))
    }
    return(d)
  }


  dt2 <- data.frame(
    iso = as.factor(rep(levels(dt$iso),length(levels(dt$month))))
    ,month = unlist(lapply(1:length(levels(dt$month)),function(m){
      rep(levels(dt$month)[m],length(levels(dt$iso)))
    }))
    ,volume = runif(length(levels(dt$month))*length(levels(dt$iso)),0,100)
  )


  # no reason to have factors
  # so I suggest converting to character
  dt2$iso <- as.character(dt2$iso)
  dt2$month <- as.character(dt2$month)

  mChoro <- Mchoropleth(
    volume ~ iso
    , data = dt2
    , pal = 'PuRd'
    , cuts = 3
    , animate = "month"
    , play = T
  )
  mChoro
于 2014-09-11T15:52:07.733 回答