Interactive Choropleth in R of Sweden

十年热恋 提交于 2020-01-14 03:11:08

问题


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)

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

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

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)

Which produces

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.


回答1:


Given

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

you could do e.g.

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)

or

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




回答2:


Given a SpatialPolygosDataFrame from getData you could use library(mapview) to do the rest for you:

library(raster)
library(mapview)

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

mapview(swe)

To control which attribute to plot use the zcol argument.



来源:https://stackoverflow.com/questions/37742879/interactive-choropleth-in-r-of-sweden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!