0
votes

I am driving myself crazy and cannot find any answers to this issue. I am trying to map the colour of the polygons (NOT the fill, which I would like to keep transparent) to the factor variable TRAIL_CLASS which has 4 levels. Below is my complete code, which gives the error message "Error: Must request at least one colour from a hue palette." I have also gotten the error "Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPolygon, : object 'TRAIL_CLASS' not found" when I have tried putting the colour and/or fill outside of the aes function. However when I intentionally set all colours to purple, it has to be outside of the aes function to work properly. Anyways, how would I be able to set the colours of the line based on the TRAIL_CLASS variable?

# 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 to GeoJSON: https://raw.githubusercontent.com/briannadrew/kingston-trails-geospatial-mapping/main/trails.geojson

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

1

1 Answers

0
votes

The issue is that you use fill=NA inside aes(). Simply passing it as argument to geom_polygon, i.e. outside of aes() will fix your issue.

# 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()