在R中使用Plotly的Choropleth贴图

在R中使用Plotly的Choropleth贴图,r,ggplot2,rstudio,plotly,R,Ggplot2,Rstudio,Plotly,我想制作一个交互式choropleth地图,这样当我在一个区域上悬停时,它将显示一个计数和我正在悬停的区域的名称。我正在使用的数据集可以在这里快速下载。 这就是我要说的 library(ggplot2) library(rgdal) utah <- readOGR(dsn= "PATH/HealthDistricts2015.shp", layer = "HealthDistricts2015") utah@data$id = rownames(utah@data) utah.point

我想制作一个交互式choropleth地图,这样当我在一个区域上悬停时,它将显示一个计数和我正在悬停的区域的名称。我正在使用的数据集可以在这里快速下载。

这就是我要说的

library(ggplot2)
library(rgdal)
utah <- readOGR(dsn= "PATH/HealthDistricts2015.shp", layer = "HealthDistricts2015")
utah@data$id = rownames(utah@data)
utah.points = fortify(utah, region="id")
utah.df = inner_join(utah.points, utah@data, 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("")
库(ggplot2)
图书馆(rgdal)
犹他州您可以与
一起使用

首先转换ArcGIS
形状
文件的坐标

lat_lon <- spTransform(utah, CRS("+proj=longlat +datum=WGS84"))
对于
hoverinfo
我们只需为每个区域的质心添加一个点

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

代码> P可以考虑使用传单吗?检查这个链接完美!谢谢
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))
p <- add_trace(p, 
               type='scattergeo',
               x = lat_lon@polygons[[i]]@labpt[[1]],
               y = lat_lon@polygons[[i]]@labpt[[2]],
               showlegend = FALSE,
               text = lat_lon@data[[1]][[i]],
               hoverinfo = 'text',
               mode = 'markers'
               )
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 = lat_lon@polygons[[i]]@labpt[[1]],
                 y = lat_lon@polygons[[i]]@labpt[[2]],
                 showlegend = FALSE,
                 text = lat_lon@data[[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)

             )