0

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

Subset of ne_area:  "pnw"

[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.

o'blah
  • 1
  • 1
  • Not sure about your main problem, but one hint is that instead of `unit_name == "Mount Rainier"|unit_name== "Lake Chelan NRA"...` you can use `unit_name %in% c("Mount Rainier", "Lake Chelan NRA", "North Cascades",...)` – Gregor Thomas Mar 30 '20 at 19:32
  • Can you please do `dput(ne_area)` and paste the output here? THen someone can reproduce the problem and be able to provide a solution. – massisenergy Mar 30 '20 at 20:01
  • Not sure where to paste that. – o'blah Mar 30 '20 at 21:58
  • Using %n% as suggested brought the same results (legend with colors but no names) – o'blah Mar 30 '20 at 22:08
  • The output for dput(ne_area) is 74,000 characters and too long to paste in the comment or question section – o'blah Mar 30 '20 at 22:17
  • Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). `reproduce()` or using `dput()` for smaller chunks of data. `?dput` is useful too... – massisenergy Mar 30 '20 at 22:25
  • This may take some time on my end. I emailed my prof to ask which parts I should include. It's 9 columns over lots of lat long coordinates – o'blah Mar 30 '20 at 22:58
  • "Crater Lake NP" is the first alphabetically in `unit_name`. Looking at [similar](https://github.com/tidyverse/ggplot2/issues/2531) [issues](https://stackoverflow.com/questions/36829610/color-error-in-ggplot2-error-in-grdevicescol2rgbcolour-true-invalid-rgb) reported elsewhere, my guess is that `pnw$unit_name`, since it's an "sf" data.frame, is causing some issues for grDevices. What is the result of `class(pnw$unit_names)`? Can you coerce to a factor and then try? (as in: `pnw$unit_names <- as.factor(pnw$unit_names)`). – chemdork123 Mar 31 '20 at 06:46
  • class(pnw$unit_names) is a factor – o'blah Apr 01 '20 at 03:37
  • I added a link to the dataset in my question – o'blah Apr 01 '20 at 03:37

0 Answers0