2016-06-10 119 views
1

我正試圖在R中的一個Shiny應用程序中開發一個交互式Choropleth。我已經嘗試過使用gVis和rCharts,但仍然沒有任何運氣。我現在需要爲瑞典想象它,但我可能稍後需要它爲其他國家。這是我到目前爲止的gvisGeoMap:瑞典R中的交互式Choropleth

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) 

根據相關文檔,我應該能夠使用lattitude和經度座標,因爲我想在這裏,但我還沒有成功呢。我使用的shapefile是從http://www.gadm.org/download

基本上,有沒有人知道如何獲得一個交互式可視化工作與gadf.org形狀文件?

這是我如何與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,但瑞典當然。

回答

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([email protected], htmltools::htmlEscape(sprintf("%s: %s", NAME_1, Patients))) 
) 

enter image description here

+0

這正是我所期待的。我用了相當長的時間來弄清楚爲什麼Leaflet地圖不能在我的Shiny應用中渲染。事實證明,Leaflet和rCharts/NVD3之間有一些衝突,我必須在解決之前解決它。但現在呢!謝謝。 – marcopah

1

鑑於從getData一個SpatialPolygosDataFrame你可以使用library(mapview)爲你做的其餘部分:

library(raster) 
library(mapview) 

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

mapview(swe) 

要控制要繪製哪個屬性,請使用zcol參數。