I am having difficulty displaying the park names next to the color of each park on the legend. Park names are in a dataframe called pnw under the column "unit_name."
Map image 1 Protected Areas of the Pacific Northwest
[Link to my github datasets.] (https://github.com/githasg/geog473-673/tree/master/datasets) The ne_10m_parks_and_protected_lands and ne_10m_admin_1_states_provinces files contain the shapefile contents of ne_area and states.
ne_area.shp <- readOGR("/Users/MainFile/Documents/Courses/Advanced_R/Datasets/ne_10m_parks_and_protected_lands/ne_10m_parks_and_protected_lands_area.shp")
class(ne_area.shp) #Good, Polygons dataframe
ne_area.shp@proj4string
ne_area = st_as_sf(ne_area.shp) #convert ne_area.shp to sf object
class(ne_area) # "sf" data.frame
states <-readOGR("/Users/MainFile/Documents/Courses/Advanced_R/Datasets/ne_10m_admin_1_states_provinces")
class(states) #Spatial Polygon Dataframe
states = st_as_sf(states) #Convert states to sf object
class(states) # "sf" data.frame
pnw.shp <- subset(ne_area, unit_name == "Mount Rainier"|unit_name== "Lake Chelan NRA"|unit_name == "North Cascades"|unit_name == "Ross Lake NRA"|unit_name == "Olympic NP"|unit_name == "Craters of the Moon NM & PRES"|unit_name == "Glacier NP"|unit_name == "North Cascades NP"| unit_name == "Crater Lake NP"| unit_name == "Redwood NP"| unit_name == "Yellowstone NP"| unit_name =="Grand Teton NP")
pnw = st_as_sf(pnw.shp)
class(pnw) # "sf" data.frame
ggplot(data = states) + geom_sf() +
geom_sf(data = pnw, aes(color = unit_name, fill = unit_name)) +
coord_sf(xlim = c(-127, -110), ylim = c(40,50), expand = FALSE) +
theme(
panel.ontop = TRUE, ## Note: this is to make the panel grid visible in this example
panel.grid = element_blank(),
line = element_blank(),
rect = element_blank(),
text = element_blank(),
plot.background = element_rect(fill = "transparent"),
panel.grid.major = element_line(colour = "transparent")
)
I've tried adding the following with no useful results:
guides(fill = guide_legend(label = TRUE)) +
geom_sf_label(aes(label= unit_name, size = 10)) +
scale_fill_discrete(labels = c('Mount Rainier', 'Lake Chelan NRA', 'North Cascades','Ross Lake NRA', 'Olympic NP', 'Craters of the Moon NM & PRES', 'Glacier NP', 'North Cascades NP', 'Crater Lake NP', 'Redwood NP','Yellowstone NP', 'Grand Teton NP')) +
scale_fill_continuous(values = mycolours, labels = c('Mount Rainier', 'Lake Chelan NRA', 'North Cascades','Ross Lake NRA', 'Olympic NP', 'Craters of the Moon NM & PRES', 'Glacier NP', 'North Cascades NP', 'Crater Lake NP', 'Redwood NP','Yellowstone NP', 'Grand Teton NP')) +
scale_fill_continuous(labels = c('Mount Rainier', 'Lake Chelan NRA', 'North Cascades','Ross Lake NRA', 'Olympic NP', 'Craters of the Moon NM & PRES', 'Glacier NP', 'North Cascades NP', 'Crater Lake NP', 'Redwood NP','Yellowstone NP', 'Grand Teton NP')) +
scale_fill_identity("Protected Lands in the Pacific Northwest", labels = pnw, breaks = mycolours, guide = "legend") +
Error in grDevices::col2rgb(colour, TRUE) : invalid color name 'Crater Lake NP'
Any help is very much appreciated.