Keep getting error: “`data` must be a data frame, or other object coercible by `fortify()`”

> library(reshape2)
> library(data.table)
> library(ggplot2)
> library(ggmap)
> library(maps)
> library(mapdata)
> library(purrr)
> library(plyr)
> library(tidyverse)
> #made with help from R Studio Community
> # build vectors  
> county <- c(demographics_dat$
> state <- c(demographics_dat$
> pop <- c(demographics_dat$pop.size)
> poverty <- c(demographics_dat$poverty)
> # now we assemble into data frame from vectors
> pov <- data.frame(county, state, pop, poverty)
> # assembled pipe 
> stateandpov <- pov %>% group_by(state) %>% summarize(mean_pov = mean(poverty))
> #Join both mean pop and mean pov together 
> meanandpov <- join(stateandpov,stateandmeanpop, by='state')
> hist(meanandpov$mean_pov)
> plot(meanandpov$mean_pov)
> #Create dataframe for states
> states <- map_data("state")
> USA <- map_data("usa")
> dim(states)
[1] 15537     6
> #Test it out 
> ggplot(data =  states) + ggplot(data = stateandmeanpop$mean_pop) +
+   geom_polygon(aes(x = long, y = lat, fill = region, group = group), 
+                color = "white") + 
+                coord_fixed(1.3) +
+                guides(fill=FALSE)
Error: `data` must be a data frame, or other object coercible by `fortify()`, not a numeric vector

Just wondering why I keep getting the error also, first attempt at cleaning this up before posting let see how i did

okay so not good I am sorry!!!

I am sorry to everyone who has to strain to read this post

You are passing a numeric vector to the data argument of ggplot() with the above code and that is what the error refers to. That is also the second call to ggplot() in that chain of functions, which is not something I have ever seen and seems likely to throw an error in itself. What are you trying to do with that?

1 Like

I am trying to superimpose the state means that I have into each state individually. So either a line or in the middle showing the mean. Still very new to R so if you see anything I do that is not commonplace it is a result of me being a junior to R.

Could you share a sample of demographics_dat on a copy/paste friendly format?, ideally, could you turn this into a proper minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

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