2017-03-24 13 views
2

インタラクティブなchoroplethマップを作成したいので、Region上にカーソルを置くと、CountとRegionの名前が表示されます。私が扱っているデータセットはすぐにダウンロードできます。 ftp://ftp.agrc.utah.gov/UtahSGID_Vector/UTM12_NAD83/HEALTH/UnpackagedData/HealthDistricts2015/_Statewide/HealthDistricts2015_shp.zipChoroplethマップでPlotlyを使用するR

これは私が遠くまで持っているものです。

library(ggplot2) 
library(rgdal) 
utah <- readOGR(dsn= "PATH/HealthDistricts2015.shp", layer = "HealthDistricts2015") 
[email protected]$id = rownames([email protected]) 
utah.points = fortify(utah, region="id") 
utah.df = inner_join(utah.points, [email protected], by="id") 

colnames(utah.df)[8] = "Region" 
UDist <- sort(unique(as.character(utah.df$Region))) 
RegionS = data.frame(Region = UDist, Count = sample(1:length(UDist))) 

PlotData <- left_join(utah.df, RegionS, b = "Region") 


ggplot(PlotData, aes(long,lat,group=group,fill=Count)) + 
    geom_polygon() + 
    geom_path(color="black") + 
    coord_equal() + 
    theme_bw() + 
    theme(axis.text.x=element_blank(), 
     axis.ticks.x=element_blank(), 
     axis.text.y=element_blank(), 
     axis.ticks.y=element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.grid.major = element_line(colour = "white") 
     ) + 
    xlab("") + 
    ylab("") 

このコードは私に素晴らしいchoroplethマップを与えますが、私はホバリング機能を追加したいと思います。私はggplotly()を使ってみましたが、それは私に素晴らしいプロットを与えませんでした。私はplot_ly()でこれをプロットする方法を解明しようとしましたが、どこにも取得できませんでした。このようなプロットを作成する方法はありますか?私が "Count"と "Region"を表示するときに表示されますか?

+0

はたぶん、リーフレットの使用を検討しますか?このリンクを確認してくださいhttps://rpubs.com/walkerke/leaflet_choropleth – msubbaiah

答えて

1

mapboxPlotlyを一緒に使用できます。

まず

utah_geojson <- geojson_json(lat_lon) 
geoj <- fromJSON(utah_geojson) 

は次いで分離層

for (i in 1:length(geoj$features)) { 
    all_layers[[i]] = list(sourcetype = 'geojson', 
         source = geoj$features[[i]], 
         type = 'fill', 
         ) 
} 
p %>% layout(mapbox = list(layers = all_layers)) 

各地区を追加するにGeoJSONオブジェクトにデータを変換shapeファイル

lat_lon <- spTransform(utah, CRS("+proj=longlat +datum=WGS84")) 

次へのArcGISの座標を変換しますhoverinfoの場合は、各地区の電子重心

p <- add_trace(p, 
       type='scattergeo', 
       x = [email protected][[i]]@labpt[[1]], 
       y = [email protected][[i]]@labpt[[2]], 
       showlegend = FALSE, 
       text = [email protected][[1]][[i]], 
       hoverinfo = 'text', 
       mode = 'markers' 
       ) 

enter image description here

完全なコード

library(rgdal) 
library(geojsonio) 
library(rjson) 
library(plotly) 

Sys.setenv('MAPBOX_TOKEN' = 'secret_token') 

utah <- readOGR(dsn= "HealthDistricts2015.shp", layer = "HealthDistricts2015") 
lat_lon <- spTransform(utah, CRS("+proj=longlat +datum=WGS84")) 
utah_geojson <- geojson_json(lat_lon) 
geoj <- fromJSON(utah_geojson) 
all_layers <- list() 

my_colors <- terrain.colors(length(geoj$features)) 

p <- plot_mapbox() 
for (i in 1:length(geoj$features)) { 
    all_layers[[i]] = list(sourcetype = 'geojson', 
         source = geoj$features[[i]], 
         type = 'fill', 
         color = substr(my_colors[[i]], 1, 7), 
         opacity = 0.5 
         ) 
    p <- add_trace(p, 
       type='scattergeo', 
       x = [email protected][[i]]@labpt[[1]], 
       y = [email protected][[i]]@labpt[[2]], 
       showlegend = FALSE, 
       text = [email protected][[1]][[i]], 
       hoverinfo = 'text', 
       mode = 'markers' 
       ) 


} 

p %>% layout(title = 'Utah', 
      mapbox = list(center= list(lat=38.4, lon=-111), 
          zoom = 5.5, 
          layers = all_layers) 

      ) 
+0

パーフェクト!ありがとう! – ZBauc

関連する問題