I am building a simple map with 3 points that I want to have different symbols but when I try to change the shape by group manually I get an error about shape aesthetics.
First a map:
library(ggplot2)
library(maps)
library(dplyr)
county_df <- map_data('county')
countyMap <- subset(county_df, region=="ohio")
countyMap$county <- countyMap$subregion
cnames <- aggregate(cbind(long, lat) ~ subregion, data=countyMap, FUN=mean)`
lat_Gallia <- c(38.4, 39.1) #zooming in
lon_Gallia <- c(-82.0, -82.6) #zooming in
Then a dataframe of points:
object = c("dog", "human", "farm")
long = c(-82.190833, -82.243889, -82.284167)
lat = c(38.881111, 38.843056, 38.828889)
df = cbind.data.frame(object, long, lat)
Plot the map with points:
ggplot(countyMap, aes(long, lat)) +
geom_polygon(aes(group=group), colour='black', fill="white") +
coord_map(ylim = lat_Gallia, xlim = lon_Gallia) +
geom_polygon(data=countyMap_sb, aes(group=group), colour='black', fill='black', alpha = 0.25) +
geom_point(data = df, aes(long, lat), shape = object, color = object) +
scale_shape_manual(values=c(0, 1, 2))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme_minimal() +
theme(legend.position = "top")
But I get the following error:
Error in `geom_point()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 3rd layer.
Caused by error in `translate_shape_string()`:
! Shape aesthetic contains invalid values: "dog", "human", and "farm".
I developed this code using this resource and this resource. I am able to get a map with all points as the default circle points, and am able to change all of those points to different symbols, but the manual change by group still fails. I have tried a variety of different fixes like this, renaming my categories, reading them as characters or factors, or just trying to change their color even if they’re all the same shape.
When I try changing just the color with:
ggplot(countyMap, aes(long, lat)) +
geom_polygon(aes(group=group), colour='black', fill="white") +
coord_map(ylim = lat_Gallia, xlim = lon_Gallia) + #this gives the zoom area
geom_polygon(data=countyMap_sb, aes(group=group), colour='black', fill='black', alpha = 0.25) +
geom_point(data = df, aes(long, lat), color = object) +
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme_minimal() +
theme(legend.position = "top")
I get the error:
Error in `geom_point()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 3rd layer.
Caused by error:
! Unknown colour name: dog
scrletbgonia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.