ggplot2 geom_sf - легенда карты, показывающая цвета многоугольника, но не имена - PullRequest
0 голосов
/ 30 марта 2020

У меня возникают трудности с отображением названий парков рядом с цветом каждого парка в легенде. Названия парков находятся в кадре данных, называемом pnw, в столбце «имя_устройства».

Изображение карты 1 Охраняемые районы Тихого океана c Северо-запад

Subset of ne_area:

[Ссылка на мои наборы данных github.] (https://github.com/githasg/geog473-673/tree/master/datasets) Файлы ne_10m_parks_and_protected_lands и ne_10m_admin_1_states_provinces содержат содержимое шейп-файла ne_area и состояний.

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")
)  

Я попытался добавить следующее без каких-либо полезных результатов:

 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") +

Ошибка в grDevices :: col2rgb (color, TRUE): неверное имя цвета 'Кратер Lake NP '

Любая помощь очень ценится.

...