如何将 ggplot 多边形的颜色(不是填充)映射到因子变量?

How do I map the colour (not fill) of a ggplot polygon to a factor variable?

我快把自己逼疯了,找不到这个问题的任何答案。我正在尝试将多边形的颜色(不是填充,我想保持透明)映射到具有 4 个级别的因子变量 TRAIL_CLASS。下面是我的完整代码,它给出了错误消息 "Error: Must request at least an color from a hue palette." 我也得到了错误 "Error in图层(数据=数据,映射=映射,stat=stat,geom=GeomPolygon,: object 'TRAIL_CLASS' not found" 当我尝试将颜色 and/or 填充放在 aes 函数之外时。但是当我有意将所有颜色设置为紫色时,它必须在 aes 函数之外aes 函数才能正常工作。无论如何,我如何才能根据 TRAIL_CLASS 变量设置线条的颜色?

# load required libraries
library(geojsonio)
library(maps)
library(rgdal)
library(maptools)
library(ggmap)
library(RgoogleMaps)
library(sp)
library(broom)
library(dplyr)
library(plyr)
library(viridis)

# read in GeoJSON file containing spatial coordinates of Kingston trails
trail_shapes <- geojson_read("https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.geojson", what = "sp")
trail_shapes_df <- tidy(trail_shapes, type = "trail_class") # fortifying GeoJSON data file

# read in CSV file containing all attributes of trails
data <- read.table("https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.csv", header = TRUE, sep = ",")
data$TRAIL_CLASS <- as.factor(data$TRAIL_CLASS)
View(data)

# merge data frame containing GeoJSON objects with the data frame containing their attributes
trail_shapes_df <- join(trail_shapes_df, data, by = "id", type = "full")
View(trail_shapes_df)

# acquire map of Kingston
register_google(key = "omitted my API key for protection") # provide API key from Google Developers to be able to use maps
map <- get_googlemap(center = c(lon = -76.54, lat = 44.28), zoom = 12, maptype = "roadmap") # get the map at the proper location and scale
map <- ggmap(map) + ggtitle("Kingston Trails") # convert to ggmap and add title

# print map with trails on top
print(map + geom_polygon(data = trail_shapes_df, aes(fill = NA, color = TRAIL_CLASS, x = long, y = lat, group = id)) + theme_void())

Link 到 GeoJSON:https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.geojson

Link 到 CSV:https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.csv

问题是您在 aes() 中使用了 fill=NA。只需将其作为参数传递给 geom_polygon,即在 aes() 之外即可解决您的问题。

# load required libraries
library(geojsonio)
library(maps)
library(rgdal)
library(maptools)
library(ggmap)
library(RgoogleMaps)
library(sp)
library(broom)
library(dplyr)
library(plyr)
library(viridis)

# read in GeoJSON file containing spatial coordinates of Kingston trails
trail_shapes <- geojson_read("https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.geojson", what = "sp")
trail_shapes_df <- tidy(trail_shapes, type = "trail_class") # fortifying GeoJSON data file

# read in CSV file containing all attributes of trails
data <- read.table("https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.csv", header = TRUE, sep = ",")
data$TRAIL_CLASS <- as.factor(data$TRAIL_CLASS)

# merge data frame containing GeoJSON objects with the data frame containing their attributes
trail_shapes_df <- join(trail_shapes_df, data, by = "id", type = "full")

ggplot(trail_shapes_df) + 
  geom_polygon(aes(color = TRAIL_CLASS, x = long, y = lat, group = id), fill = NA) + theme_void()