瑞典R的互动式Choropleth [英] Interactive Choropleth in R of Sweden

查看:113
本文介绍了瑞典R的互动式Choropleth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R的Shiny应用程序中开发一个交互式的choropleth.我已经尝试使用plotly,gVis和rCharts,但是仍然没有任何运气.我现在需要在瑞典对其进行可视化,但稍后可能还要在其他国家/地区将其可视化.到目前为止,这是我对gvisGeoMap的了解:

I'm trying to develop an interactive choropleth in a Shiny application in R. I've tried with plotly, gVis and rCharts, but still without any luck. I need to visualise it for Sweden right now, but I probably need it for other countries as well later on. This is what I have so far for gvisGeoMap:

polygons <- readOGR("/ggshape", layer="SWE_adm1")
polygons <- fortify(polygons, region="ID_1")
data.poly <- as.data.frame(polygons)
data.poly <- data.poly[,c(1,2)]
data.poly.final <- data.frame(locationvar = paste(data.poly[,2],data.poly[,1], sep = ":"), 
                              numvar=1, 
                              hovervar="test")

data.poly.final$locationvar <- as.character(data.poly.final$locationvar)
data.poly.final$hovervar <- as.character(data.poly.final$hovervar)


map <- gvisGeoMap(data=data.poly.final, locationvar = "locationvar", 
                  options=list(width='800px',heigth='500px',colors="['0x0000ff', '0xff0000']",
                               dataMode = "markers"))
plot(map)

根据文档,我应该能够在此处尝试使用经度和纬度坐标,但是我还没有成功.我正在使用的shapefile来自 http://www.gadm.org/download

Based on the documentation, I should be able to use lattitude and longitude coordinates as I'm trying here, but I have not been successful yet. The shapefile I'm using is from http://www.gadm.org/download

基本上,没有人知道如何从gadm.org获得交互式可视化文件来处理shapefile吗?

Basically, does anyone know how to get an interactive visualisation to work with shapefiles from gadm.org?

这就是我用ggplot完成的方式

This is how I would do it with ggplot

      SWE <- fortify(polygons, region="ID_1")
      SWEplot <- merge(x=SWE, y=my_data, by="id")

        p <- ggplot() +
          geom_polygon(data = SWEplot , aes(x = long, y = lat, group = group, fill = Patients)) + 
          geom_path(color="black") + 
          theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), # get rid of x ticks/text
                axis.ticks.x = element_blank(),axis.text.x = element_blank(), # get rid of y ticks/text
                plot.title = element_text(lineheight=.8, face="bold", vjust=1),
                panel.background = element_blank(), panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                legend.text=element_text(size=14),
                legend.title=element_text(size=16)) + # make title bold and add space
          coord_equal(ratio=1)

哪个生产

根据需要,但不具有交互性.我基本上希望实现的是这样的东西 http://rcharts.io/查看器/?6735051#.V1px-7t97mE ,当然是瑞典语.

As desired but without the interactivity. What I'm essentially hoping to achieve is something like this http://rcharts.io/viewer/?6735051#.V1px-7t97mE but for Sweden of course.

推荐答案

给出

library(raster)
swe <- getData("GADM", country = "SWE", level = 1)
swe$Patients <- runif(1:nrow(swe))

您可以这样做

library(maptools)
library(rgeos)
library(broom)
library(ggplot2)
library(plotly)
swe_s <- gSimplify(swe, .01)
SWE <- fortify(swe_s, region="ID_1")
SWEplot <- merge(x=SWE, y=swe, by.x="id", by.y="ID_1")
ggplot() +
  geom_polygon(data = SWEplot , aes(x = long, y = lat, group = group, fill = Patients)) + 
  coord_quickmap() +
  ggthemes::theme_map() + theme(legend.position=c(.8, .2)) ->
  p
ggplotly(p)

library(leaflet)
pal <- scales::seq_gradient_pal(low = "#132B43", high = "#56B1F7", space = "Lab")(seq(0, 1, length.out = 255))
leaflet() %>%
  addPolygons(
    data = swe,
    color = "#000", weight = 1, opacity = 0.5,
    fillColor = ~colorNumeric(pal, swe$Patients)(Patients), fillOpacity = 1, 
    popup = with(swe@data, htmltools::htmlEscape(sprintf("%s: %s", NAME_1, Patients)))
  )

这篇关于瑞典R的互动式Choropleth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆