Interactive Choropleth in R of Sweden

好久不见. 提交于 2019-12-08 03:42:28

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

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.

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