R: State and County Outlines for choroplethrZip - r

I have a dataset with zip codes that I have used choroplethrZip to graph the data. I am looking at the data on the state and county level. However, zip codes don't necessarily correspond with state and county lines. I have tried to use reference_map = TRUE, but it doesn't have county lines, and starts to look a little busy. Is there a way to change the default reference map to one that has state and county lines without the rest of the map detail? In other words, I don't want streets and topography.
Here is my code with the sample data that is similar to the data I am using. You can see the issue with the state of Texas boundaries.
#zip.regions metadata file for choroplethrZip
data(zip.regions)
head(zip.regions)
#Test data file:A data.frame containing population estimates
# for US Zip Code Tabulated Areas (ZCTAs) in 2012.
data(df_pop_zip)
#Create a choropleth of US Zip Codes
zip_choropleth(df_pop_zip,
state_zoom="texas",
title="2012 Texas State ZCTA Population Estimates",
legend="Population",
reference_map = TRUE)
#Zoom County
dd_fips = c(48113, 48121)
zip_choropleth(df_pop_zip,
county_zoom=dd_fips,
title="2012 Denton & Dallas ZCTA Population Estimates",
legend="Population",
reference_map = TRUE)
TexasPlot
DentonDallasPlot

I used this blog and found out how to make this work: http://www.arilamstein.com/blog/2015/07/02/exploring-the-demographics-of-ferguson-missouri/
Here is my final code:
library(choroplethrZip)
library(ggplot2)
library(choroplethr)
#Pull in zip.regions metadata file for choroplethrZip
data(zip.regions)
head(zip.regions)
#Test data file:A data.frame containing population estimates
# for US Zip Code Tabulated Areas (ZCTAs) in 2012.
data(df_pop_zip)
# highlight a county
highlight_county = function(county_fips)
{
library(choroplethrMaps)
data(county.map, package="choroplethrMaps", envir=environment())
df = county.map[county.map$region %in% county_fips, ]
geom_polygon(data=df, aes(long, lat, group = group), color = "yellow", fill = NA, size = 1)
}
#Zoom County
dd_fips = c(48113, 48121)
zip_choropleth(df_pop_zip,
county_zoom=dd_fips,
title="2012 Denton & Dallas ZCTA Population Estimates",
legend="Population",
reference_map = TRUE) +
highlight_county(dd_fips)
Rplot Counties
I just had to add in the highlight_county function and it worked great. When I tested with my data (instead of the generic population data) it worked as well.

Related

Learning How to Use Shapefiles in R

I am trying to learn more about "shapefiles" and how to plot them in R. Ideally, I would like to make a "leaflet map" (interactive or static) that looks something like this: https://i.cbc.ca/1.5587407.1591305080!/fileImage/httpImage/covid-19-cases-in-the-city-of-toronto-since-may-1.jpg
For this example, I found a publicly available dataset that contains information about Canadian Postal Codes - I created a new random variable that represents "number of hockey players" in each postal code:
# download folder from here : https://www.serviceobjects.com/blog/free-zip-code-and-postal-code-database-with-geocoordinates/
#extract canadian part (CanadianPostalCodes202204) - call it "canada"
canada = read.csv("CanadianPostalCodes202204.csv")
# extract Ontario
some_prov <- canada[which(canada$PROVINCE_ABBR == "ON"), ]
# create some random variable for this example
some_prov$number_of_hockey_players = as.integer(rnorm(nrow(some_prov), 87, 10))
POSTAL_CODE CITY PROVINCE_ABBR TIME_ZONE LATITUDE LONGITUDE number_of_hockey_players
11 L7C 2T6 CALEDON EAST ON 5 43.81709 -79.81907 88
17 M1K 1W6 SCARBOROUGH ON 5 43.71187 -79.26526 98
21 N5Y 4N3 LONDON ON 5 43.02114 -81.22003 72
22 N4N 1J3 HANOVER ON 5 44.14945 -81.03273 87
24 N2M 3R9 KITCHENER ON 5 43.43482 -80.48112 99
26 L1C 0E4 BOWMANVILLE ON 5 43.92937 -78.68348 90
I know how to make a general leaflet map for this data - this looks something like this:
library(leaflet)
some_prov %>%
leaflet() %>%
addTiles() %>%
addMarkers(clusterOption=markerClusterOptions())
I would now like to make a map similar to the COVID map above. Doing some research online, it seems that making such a map might require a "shapefile", or some information that tells the map where to put the desired boundaries. For example, I found the following website that might be useful (https://www.gis-blog.com/create-a-leaflet-map-using-r/ - I changed "USA" to "CANADA"):
#load leaflet package for R
library(leaflet)
library(raster)
#arbitrary data (code from above)
canada <- getData("GADM", country="canada", level=2)
canada$number_of_hockey_players <- as.integer(rnorm(n=nrow(canada), 150, 30))
#create a color palette to fill the polygons
pal <- colorQuantile("Greens", NULL, n = 5)
#create a pop up (onClick)
polygon_popup <- paste0("<strong>Name: </strong>", canada$NAME_1, "<br>",
"<strong>Indicator: </strong>", round(canada$number_of_hockey_players,2))
#create leaflet map
map = leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.35, 39.7,
zoom = 4) %>%
addPolygons(data = canada,
fillColor= ~pal(number_of_hockey_players),
fillOpacity = 0.4,
weight = 2,
color = "white",
popup = polygon_popup)
map
But I am not sure if this is the best way to do things.
I think I found some publicly available shapefile over here : https://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2011-eng.cfm - but I am not sure how to correctly use these files to accomplish my goal. I am currently searching for other relevant shapefiles.
Can someone please show me how I can take this dataset that I created and make a map (interactive or static) similar to the COVID map above (with either postal code boundaries, city boundaries, etc.) ?
Thank you!

R: Overlay state map on longitude/latitude plot

I have produced a plot of zipcodes on a grid of longitude and latitude. However, I would like to add the outline for the state of Pennsylvania to this plot. I am not sure how to go about doing this. Here is the code I have so far:
library(tidyverse)
library(zipcode)
data(zipcode)
toplot <- merge(zipcode, nhmzipuse, sort = FALSE, by.x = "zip", by.y = "zip")
ggplot(toplot, aes(longitude, latitude)) + geom_point(aes(color=count))
Here is what my plot looks like currently:
I know that I need to somehow get data for the border of the state of Pennsylvania and merge it with my data, but I'm not sure how to go about doing that.
Any recommendations are much appreciated!

Colorize the map of Russia depending on the variable in R

I have a map of Russia with regional subdivision
library(raster)
data <- getData('GADM', country='RUS', level=1)
http://www.gks.ru/bgd/regl/B16_14p/IssWWW.exe/Stg/d01/08-01.doc
The link is to a Word.doc with data (table) on crime rates for Russian regions. I can extract this data and use it in R. I want to take 2015 year and colorize regions on the map depending on the crime rate (also add a legend). How can I do this? The problem is that names of regions are sometimes different in the shape file (NL_NAME_1) and in the data from www.gks.ru.
I also have this code for graph that I need, except that here we have meaningless colors:
library(sp)
library(RColorBrewer)
data$region <- as.factor(iconv(as.character(data$NAME_1)))
spplot(data, "region", xlim=c(15,190), ylim=c(40,83),
col.regions=colorRampPalette(brewer.pal(12, "Set3"))(85), col = "white")
If I understand your question properly, you just need to add your data to the spatial object for making colors meaningful.
Note, please, that the data is a reserved word in R. So, it's better to modify a little your variable name:
geo_data <- getData('GADM', country = 'RUS', level = 1)
Let's emulate some data to demonstrate a visualization strategy:
set.seed(23)
geo_data#data["data_to_plot"] <- sample(1:100, length(geo_data#data$NAME_1))
Using a default GADM projection would cut the most eastern part of the country. A simple transformation helps to fit the whole area to a plot:
# fit Russian area inside the plot
geo_data_trsf <- spTransform(geo_data, CRS("+proj=longlat +lon_wrap=180"))
Draw the map selecting data_to_plot instead of region:
max_data_val <- max(geo_data_trsf#data$data_to_plot)
spplot(geo_data_trsf, zcol = "data_to_plot",
col.regions = colorRampPalette(brewer.pal(12, "Set3"))(max_data_val),
col = "white")
The plot limits are adjusted automatically for the transformed spatial data geo_data_trsf, making possible to omit xlim and ylim.
As for the problem with the names, I can't provide any ready-to-use solution. Obviously, the regions' names of NL_NAME_1 need some additional treatment to use them as labels. I think, it would be better to use NAME_1 as an identifier in your code to ensure that it'll be no troubles with encoding. The NL_NAME_1 column is perfectly suitable to set the correspondence between your Word-data and the data inside the spatial object geo_data.

How to map a single US state (MO) county population data using maps package?

I'm having difficulty mapping gradient colors to some county-level population data I have using the base R package maps. I know that colors must be interpolated to the dataframe, but I'm not sure how that is then translated to the map. Here is the code I'm using:
library(dplyr)
require(maps)
my_fake_data <- data_frame(
county = sample(c('list','of','all','counties'),115,T),
county_population = sample(1:1000000,115,T))
grey_black <- colorRampPalette(c('grey50','black'))
map_data <- my_fake_data %>%
arrange(county_population) %>%
mutate(county_population_color = grey_black(nrow(.)))
map('county','missouri',interior = T,fill =T,
col = grey_black(map_data$county_population_color))
How do I tell R to map colors in the correct order? My sense tells me to attach my data to map's internal database, but I can't find the documentation to do it correctly - - or, more likely, I'm just wrong. Any help would be greatly appreciated.
To answer your question, you will need to access the county.fips data frame contained in the maps package. This will have the fips number and the state, county name. This list is in the correct order for mapping. The code example below extracts the Missouri counties and randomly colors a couple for verification:
mocounties<-county.fips[grepl('missouri', county.fips$polyname),]
mocounties$col<-"grey"
mocounties$col[5]<-"red"
mocounties$col[15]<-"green"
mocounties$col[115]<-"blue"
map('county','missouri', interior = T,fill =T,
col = mocounties$col)

R overlay counties (or othr options for county_choropleth)

Using some code I've gotten from StackOverflow, I would like to shade a map but also have the county name as well.
In a perfect world, I'd like to be able to show ONLY the top and bottom 5 county names and as an additional twist I'd like the high names in red and low names in black.
But I'm having enough trouble getting any names on or finding any examples of other options to add.
I'm new to R and not sure if this is the right way to do it.
Also, is there a visualization you would recommend to show 3 variables (income, birth rates, population) by county like this?
Thank you
library(noncensus)
library(zipcode)
library(choroplethr)
library(ggplot2)
library(sp)
library(maps)
#this guy is pretty but needs county names
data(df_pop_county)
county_choropleth(df_pop_county,
title="US 2012 County Population Estimates",
legend="Population",
buckets=1,
zoom=c("new york"))
#this guy has county names
getLabelPoint <- # Returns a county-named list of label points
function(county) {Polygon(county[c('long', 'lat')])#labpt}
df <- map_data('county', 'new york') # NY region county data
centroids <- by(df, df$subregion, getLabelPoint) # Returns list
centroids <- do.call("rbind.data.frame", centroids) # Convert to Data Frame
names(centroids) <- c('long', 'lat') # Appropriate Header
map('county', 'new york')
text(centroids$long, centroids$lat, rownames(centroids), offset=0, cex=0.4)

Resources