Making maps in R

Hi everyone. I am trying to make a map in R. I want to display my output with a gradient fill, for example light colors for smaller populations in US states, and darker color for larger populations in US states (in the following code Ive changed the variable name to "variablename"). I was able to run the code without errors in previous versions or R, but now I keep running into this error:

"Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (790861): fill
Run rlang::last_error() to see where the error occurred."

Here is my code:

a <- ggplot(map, aes(long, lat, group = group))+
geom_polygon(aes(fill ="variablename"), color="black")+
scale_fill_gradient(aes(low="white", high="navy"))+
theme_void() +
theme(plot.margin = unit(c(1,1,1,1), "cm"))

Im using a map with 10 different counties which has different values, so its really not an option to specify a color for each one. Does anyone know a way around this?

Best wishes

in general an aes param that you set would not be a quoted string as this would just literally be the quoted string... in your case a single character string with exactly the content of 'variablename' and which is therefore length 1;
for it to take the value contained in a variable within the data, you should write simply the plain variable name without quote marks. like you did with long lat and group

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.