瑞典R的互动合唱

时间:2016-06-10 07:55:36

标签: r plotly rcharts choropleth gvisgeomap

我正试图在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)

哪个产生

enter image description here

根据需要但没有交互性。我基本上希望实现的是http://rcharts.io/viewer/?6735051#.V1px-7t97mE,但瑞典当然。

2 个答案:

答案 0 :(得分:4)

鉴于

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)

enter image description here

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

enter image description here

答案 1 :(得分:1)

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

library(raster)
library(mapview)

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

mapview(swe)

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