r - 如何将 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
解决方案
问题是你使用fill=NA
inside 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()
推荐阅读
- windows - Electron:在图标上拖放文件
- macos - 在控制台 hello world 应用程序中运行代码时未显示 NSNotification
- android - Android Studio 3.5 ...升级后出现错误默认活动未找到
- flutter - 如何使用 PageView.builder 为新日期显示新页面
- flutter - 当我触摸浮动按钮时如何停止flutter_webview的WebView的触摸响应
- ios - X 轴值重叠 - iOS 图表
- javascript - 如何制作多级上下文菜单?
- javascript - 我想通过ajax调用从地图中获取json数据
- python - Python Pandas:根据选定列的最重复值的降序限制1返回多列值
- python - 尝试使用标签时出现问题