用R中的工具提示绘制县级数据 [英] Plot county level data with tooltips in R

查看:133
本文介绍了用R中的工具提示绘制县级数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在美国的县级www.betydb.org上看到了一个互动式的等值线地图。我想用R重现一个类似的地图。我只是想要地图和工具提示(并非所有的tile在不同的缩放级别,或者切换地图的能力)。

地图目前在表明这可能是不可能的,但文档太广泛了,以致于我不清楚我的其他选项是什么。 / p>

那么,有没有一种方法可以在县级获得带有工具提示和着色的地图?

解决方案

这是一个来自2013年的问题。我不确定传单包是否已经出来了。现在是2017年底,并且有可能实现您的任务。如果您仍然需要执行类似的任务,我想为您保留以下内容。在这种情况下,数据集中有一些缺失的县。这些县在美国多边形数据中存在,但是它们在 mydata 中缺失。因此,我使用 setdiff() bind_rows() mydata C $ C>。当您绘制小册子地图时,您需要指定您的调色板。 Avg_yield 是一个连续变量。所以你使用 colorNumeric()

 库(栅格)
库(小册子)
library(tidyverse)

#获取美国多边形数据
美国< - getData(GADM,country =usa,level = 2)

###获取数据
mydata< - read.csv(https://www.betydb.org/miscanthus_county_avg_yield.csv,
stringsAsFactors = FALSE)%>%
dplyr :: select(COUNTY_NAME,Avg_yield)

###检查在美国存在但不在mydata中的县
###创建一个虚拟数据框并将其与mydata $绑定b
$ b mydata< - data.frame(COUNTY_NAME = setdiff(USA $ NAME_2,mydata $ COUNTY_NAME),
Avg_yield = NA,
stringsAsFactors = FALSE)%>%
bind_rows(mydata)

###创建一个调色板
mypal< - colorNumeric(palette =viridis,domain = mydata $ Avg_yield)

传单()%>%
addProviderTiles(OpenStreetMap.Mapnik)% >%
setView(lat = 39.8283,lng = -98.5795,zoom = 4)%>%
addPolygons(data = USA,stroke = FALSE,smoothFactor = 0.2,fillOpacity = 0.3,
fillColor =〜mypal(mydata $ Avg_yield),
popup = paste(Region:,USA $ NAME_2,< br>,
Avg_yield:,mydata $ Avg_yield < br>))%>%
addLegend(position =bottomleft,pal = mypal,values = mydata $ Avg_yield,
title =Avg_yield,
opacity = 1 )




I have seen an interactive choropleth map at the US county level at www.betydb.org. I would like to reproduce a similar map using R. I just want the map and the tooltips (not all of the tiles at different zoom levels, or the ability to switch maps)

The map is currently created in ruby, and the popup (in the bottom left) queries a MySQL database. The programmer who wrote it has moved on, and I am not familiar with Ruby.

Here, I will start with a csv file. The data include state and county names, and state and county FIPS. I would like to plot Avg_yield.

mydata <- read.csv("https://www.betydb.org/miscanthus_county_avg_yield.csv")
> colnames(mydata)
 [1] "OBJECTID"    "Join_Count"  "TARGET_FID"  "COUNTY_NAME" "STATE_NAME"  "STATE_FIPS" 
 [7] "CNTY_FIPS"   "FIPS"        "Avg_lat"     "Avg_lon"     "Avg_yield"  

I can plot at the state level using the googleVis package

library(googleVis)
p <- gvisGeoChart(data = mydata, locationvar="STATE_NAME", colorvar = 'Avg_yield',
                  options= list(region="US", displayMode="regions", 
                  resolution="provinces"))
plot(p)

This provides state-level coloring. My question here is, how can I get something like this with color and tooltips at county-level (rather than state-level) resolution?

The gvisGeoChart help (under region and resolution) and the Google chart documentation indicate that this may not be possible, but the documentation is so extensive that it is not clear what my other options are, within R.

So, is there a way to get a map with tooltips and coloring at county-level?

解决方案

This is a question coming from 2013. I am not sure if the leaflet package was out back then. It is the end of 2017 now, and it is possible to achieve your task. I want to leave the following for you, if you still need to do similar tasks. In this case, there are some missing counties in the data set. These counties exsit in the USA polygon data, but they are missing in mydata. So I added these counties to mydata using setdiff() and bind_rows(). When you draw a leaflet map, you need to specify your color palette. Avg_yield is a continuous variable. So you use colorNumeric(). I leave a screen shot showing a part of the leaflet map.

library(raster)
library(leaflet)
library(tidyverse)

# Get USA polygon data
USA <- getData("GADM", country = "usa", level = 2)

### Get data
mydata <- read.csv("https://www.betydb.org/miscanthus_county_avg_yield.csv",
                   stringsAsFactors = FALSE) %>%
          dplyr::select(COUNTY_NAME, Avg_yield)

### Check counties that exist in USA, but not in mydata
### Create a dummy data frame and bind it with mydata

mydata <- data.frame(COUNTY_NAME = setdiff(USA$NAME_2, mydata$COUNTY_NAME),
                     Avg_yield = NA,
                     stringsAsFactors = FALSE) %>%
          bind_rows(mydata)

### Create a color palette
mypal <- colorNumeric(palette = "viridis", domain = mydata$Avg_yield)

leaflet() %>% 
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lat = 39.8283, lng = -98.5795, zoom = 4) %>%
addPolygons(data = USA, stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.3,
            fillColor = ~mypal(mydata$Avg_yield),
            popup = paste("Region: ", USA$NAME_2, "<br>",
                          "Avg_yield: ", mydata$Avg_yield, "<br>")) %>%
 addLegend(position = "bottomleft", pal = mypal, values = mydata$Avg_yield,
           title = "Avg_yield",
           opacity = 1)

这篇关于用R中的工具提示绘制县级数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆