首页 > 解决方案 > 如何将 ggplot 多边形的颜色(非填充)映射到因子变量?

问题描述

我快把自己逼疯了,找不到这个问题的任何答案。我正在尝试将多边形的颜色(不是填充,我想保持透明)映射到具有 4 个级别的因子变量TRAIL_CLASS 。下面是我的完整代码,它给出了错误消息“错误:必须从色调调色板中请求至少一种颜色。” 我还收到错误“图层错误(数据=数据,映射=映射,stat = stat,geom = GeomPolygon,:找不到对象'TRAIL_CLASS'”当我尝试将颜色和/或填充放在 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())

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

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

标签: rggplot2colorsmappinggeojson

解决方案


问题是你使用fill=NAinside aes()。只需将它作为参数传递给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()


推荐阅读