Error Message: Aesthetics

Hi everyone! I am trying to use RStudio and the console keeps giving me an error message reading:
"Error: Aesthetics must be either length 1 or the same as the data (15537): fill"
I am trying to make a gradient map of the percent of people who regularly attend church in each state. I have attached my script below. what is the problem with my script? Thanks in advance!

religion <- usa_base +
geom_polygon(data = usa_df, aes(fill = State.religious.$Percent.Regularly.Atteding.Church), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +

Things I would check:

  1. Does the code work if you remove the . just before the $ in State.religious.$Percent.Regularly.Atteding.Church
  2. Is
nrow(usa_df) == nrow(State.religious)



To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

As @andresrcs says a reprex would be useful here, but I'm going to make a couple of guesses.

You use geom_polygon, so usa_df I assume is a data.frame with polygon definitions in it - so a row corresponding to each corner or the state level polygon - ie a lot of points per state and a state identifier. Resulting in a data.frame with 15537 rows.

Then your church attendance data is in another data.frame, with presumably one row per state, so they don't match up, and hence the error. You could merge the two data.frames so that every line in the polygon data.frame also contains the church attendance info. But that would be replicating the attendance data, and might get big if you have multiple variables you want to look at.

An alternative, which I would favour, would be to familiarise yourself with the sf package and create an sf object with one row per state with the polygon information in the geometry column of this object and the other info in the other columns, giving easy usage through geom_sf.

I'd also check your spelling of variables, I think there should be an n in ...Atteding.Church, but that might be right and reflect the original data source.

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.