将R和leafleft用于随时间变化的地图

时间:2017-09-12 14:36:10

标签: r leaflet

我有一个由某个城市的社区组织的数据库。该数据包含了几个月的经济信息。我的数据库看起来像这样(对于一个可重复的例子,3年)

> head(data , 4)
neigh_id_shape  m1_y1  m2_y1 ....  m11_y3  m12_y3 
          id 1      7      6          -1       -3            
          id 2      3     -1          -5        3            
          id 3     -1      6           0       17            
          id 4      0      3           1        2            

除此之外,我还有一个城市的shapefile对象和他们的neighboords。 " neigh_id_shape"数据中的变量是匹配变量。使用此shapefile,我可以使用leafleft

绘制自定义地图
> n_shape <- readShapeSpatial("path\\my_shape.shp") 

leaflet()  %>% addTiles() %>% setView(lng = center_city_long , lat= center_city_lat ,zoom=11) %>% addPolygons( data= n_shape , weight= 1  ) 

所以,我想做一个动态地图(带有一个播放按钮),可以显示随时间的变化(沿着月份变量)。此外,我想为月份的值构建两个颜色比例:红色比例和蓝色比例分别为负值和正值。

根据一个人的说法,我尝试了这个...但它不起作用(见剧本后的图片)

    library(htmlwidgets)
    library(htmltools)
    library(leaflet)
    #install.packages("geojsonio")
    library(geojsonio)

    llat<- c(-22.973897, -22.972892,-22.971588, 22.972270,-22.974700,-22.969869,-22.970847,-22.968832,-22.974660,-22.975243)

    llong<- c( -43.185296,-43.187338,-43.185675,-43.185869,-43.190858,-43.186791, -43.188980,-43.187092,-43.193239,-43.193486)


    #Build data.frame with 10 obs + 3 cols
power <- data.frame(
  "Latitude" = llat,#c(33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167, 54.140586, 54.140586, 48.494444, 48.494444),
  "Longitude" = llong,#c(129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889, 13.664422, 13.664422, 17.681944, 17.681944),
  "start" = do.call(
    "as.Date",
    list(
      x = c("1jan1960", "2jan1960", "31mar1960", "30jul1960", "1jan1961", "2jan1961", "31mar1961", "30jul1961" , "1jan1962", "2jan1962" ),
      format = "%d%b%Y"
    )
  )
)

# set start same as end
#  adjust however you would like
power$end <- power$start + 30


# use geojsonio to convert our data.frame
#  to GeoJSON which timeline expects
power_geo <- geojson_json(power[,c(1,2)],lat="Latitude",lon="Longitude", pretty = T)

# create a leaflet map on which we will build
leaf <- leaflet() %>%
  addTiles()

# add leaflet-timeline as a dependency
#  to get the js and css
leaf$dependencies[[length(leaf$dependencies)+1]] <- htmlDependency(
  name = "leaflet-timeline",
  version = "1.0.0",
  src = c("href" = "http://skeate.github.io/Leaflet.timeline/"),
  script = "javascripts/leaflet.timeline.js",
  stylesheet = "stylesheets/leaflet.timeline.css"
)

# use the new onRender in htmlwidgets to run
#  this code once our leaflet map is rendered
#  I did not spend time perfecting the leaflet-timeline
#  options
leaf %>%
  setView(lng = -43.187574 , lat= -22.971875,zoom=14) %>%
  onRender(sprintf(
    '
    function(el,x){
    var power_data = %s;

    var timelineControl = L.timelineSliderControl({
    formatOutput: function(date) {
    return new Date(date).toString();
    }
    });

    var timeline = L.timeline(power_data, {
    pointToLayer: function(data, latlng){
    var hue_min = 120;
    var hue_max = 0;
    var hue = hue_min;
    return L.circleMarker(latlng, {
    radius: 10,
    color: "hsl("+hue+", 100%%, 50%%)",
    fillColor: "hsl("+hue+", 100%%, 50%%)"
    });
    },
    steps: 1000,
    duration: 10000,
    showTicks: true
    });
    timelineControl.addTo(HTMLWidgets.find(".leaflet").getMap());
    timelineControl.addTimelines(timeline);
    timeline.addTo(HTMLWidgets.find(".leaflet").getMap());
    }
    ',
        power_geo
    ))

enter image description here

0 个答案:

没有答案