瑞典 R 中的交互式 Choropleth

Interactive Choropleth in R of Sweden

我正在尝试在 R 的 Shiny 应用程序中开发一个交互式等值线。我已经尝试过 plotly、gVis 和 rCharts,但仍然没有任何运气。我现在需要为瑞典形象化它,但以后我可能也需要为其他国家使用它。这是我到目前为止对 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

基本上,有谁知道如何使用来自 gadm.org 的 shapefile 进行交互式可视化?

这就是我使用 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/viewer/?6735051#.V1px-7t97mE 但当然是瑞典。

给定

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

给定来自 getData 的 SpatialPolygosDataFrame,您可以使用 library(mapview) 为您完成剩下的工作:

library(raster)
library(mapview)

swe <- getData("GADM", country = "SWE", level = 1)

mapview(swe)

要控制绘制哪个属性,请使用 zcol 参数。