【发布时间】:2020-11-22 21:44:24
【问题描述】:
我快把自己逼疯了,找不到这个问题的任何答案。我正在尝试将多边形的颜色(不是填充,我想保持透明)映射到具有 4 个级别的因子变量 TRAIL_CLASS。下面是我的完整代码,它给出了错误消息“错误:必须从色调调色板中请求至少一种颜色。”我也收到了错误“图层错误(数据=数据, 映射 = 映射, stat = stat, geom = GeomPolygon, : 当我尝试将颜色和/或填充放在 aes 函数之外时,找不到对象 'TRAIL_CLASS'"。但是,当我故意将所有颜色设置为紫色时,它必须在 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
【问题讨论】:
标签: r ggplot2 colors mapping geojson