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 inmydata
. So I added these counties tomydata
usingsetdiff()
andbind_rows()
. When you draw a leaflet map, you need to specify your color palette.Avg_yield
is a continuous variable. So you usecolorNumeric()
. I leave a screen shot showing a part of the leaflet map.