I am using choroplethr to create a climate map for the Western half of the US (state and county). The map is finished and looks amazing. Now I need to plot occurrence points (lon/lat) to the map. I tried many functions (lat/lon and lon/lat, both). Unfortunately, I didn't find a solution up to now. How could this be done?
PS (Mar 18, 2018): I have tried (e.g.) the following statement to plot the occurrence points, it seemed to be accepted, no errors, but no reaction as well: NAM_prt = NAM_prt + geom_point(data=spec_US_df, aes(x = lon, y = lat), color="red", size=30, alpha=0.5)
library(choroplethr)
# Preparing the input data.frame containing "region" and "value"
state = c('ARI')
county = c('Apache', 'Cochise', 'Coconino', 'Gila', 'Graham', 'Greenlee', 'La Paz', 'Maricopa', 'Mohave', 'Navajo', 'Pima', 'Pinal', 'Santa Cruz', 'Yavapai', 'Yuma')
region = c(4001, 4003, 4005, 4007, 4009, 4011, 4012, 4013, 4015, 4017, 4019, 4021, 4023, 4025, 4027)
value = c(40, 88, 19, 10, 10, 0, 0, 302, 47, 0,222, 9, 0, 34, 40)
SWNAMcounties = data.frame(state = state, county = county, region = region, value = value)
# Preparing the choropleth map
NAM_prt = county_choropleth(SWNAMcounties,
title = "County Climate Map (Data from 1990 to 1995)",
legend = "Impact",
num_colors = 1,
state_zoom = c("arizona"))
NAM_prt
# part 2:
# Creating occurrence points (lat/lon)
lat = c('32.22528', '32.36194', '32.66156', '32.38121', '32.69486', '32.36842', '32.51652')
lon = c('-111.1206', '-109.6742', '-110.1742', '-109.6278', '-109.4554', '-109.5601', '-110.0397')
spec_US_df = data.frame(lat, lon)
strong text
In choroplethr, All <region>_choropleth
functions return ggplot2 objects. This means that you can add another layer (such as points, in your case) with the +
operator.
First, I see an issue with how you define spec_US_df
. Namely, latitude and longitude values are supposed to be numeric, but you made them strings. So first I first rewrote your data creation code like this
# part 2:
# Creating occurrence points (lat/lon)
lat = c(32.22528, 32.36194, 32.66156, 32.38121, 32.69486, 32.36842, 32.51652)
lon = c(-111.1206, -109.6742, -110.1742, -109.6278, -109.4554, -109.5601, -110.0397)
spec_US_df = data.frame(lat, lon)
Next, it helps to actually generate the layer you want before combining it with choroplethr. Here is what I have:
library(ggplot2)
ggplot(spec_US_df) +
geom_point(mapping = aes(lon, lat), color="red", size = 30, alpha=0.5)
Finally, combine the two:
NAM_prt +
geom_point(data = spec_US_df, mapping = aes(lon, lat), color="red", size = 30, alpha=0.5, inherit.aes = FALSE)
The key difference is that when combining the original ggplot2 code with choroplethr, you also need to add the parameter inherit.aes = FALSE
. This is because choroplethr has an aesthetic parameter that you don't need (it groups polygons by state and county, which is irrelevant to your scatterplot).