Objective: Adding density to a map
Tested Packages: ggplot, ggmap, sp, rgdal
Based on the rdgal package, I have a map of a specific state.
library(rgdal)
us<-getData('GADM', country='USA', level=2)
WV <-subset(us, NAME_1=="West Virginia")
plot(WV)
I want to plot some prescription data on the map. Based on the geocode() method, specified in the following post: R convert zipcode or lat/long to county, I converted the county name to a set of latitude and longitude coordinates.
library(ggmap)
WV.opioid <- opioid.dat[opioid.dat$State.Name == "West Virginia",]
WV.coordinates <- geocode(as.character(WV.opioid$County.Name))
WV.opioid <- cbind(WV.opioid, WV.coordinates)
WV.opioid <- WV.opioid[which(!is.na(WV.opioid$lat)),]
rownames(WV.opioid) <- NULL
I have tried to plot one column of the dataset on the map:
library(ggplot2)
#opioid data from following link: https://www.datafiles.samhsa.gov/study-series/national-survey-drug-use-and-health-nsduh-nid13517
opioid.map <- ggplot(WV.opioid, aes(x = lon, y = lat,
fill = cut_number(Diff.Opioid.Prescription.Rates,
5))) +
geom_polygon(color = "gray10", size = 0.2) +
coord_equal() +
viridis::scale_fill_viridis(discrete = TRUE) +
labs(title = "Test",
fill = NULL) +
theme_void() +
theme(legend.position = "bottom",
panel.background = element_rect(fill = NA, colour = "#cccccc"))
opioid.map
I am not sure how to fix this plot and would appreciate help. I have also tried to use the rdgal package and add my data to a SpatialPoints object but have not been successful.