rggplot2tidycensus

Troubleshooting why ggplot() isn't plotting datase


I have been working with some tidycensus data for an assignment and have gotten to the point where trying to generate a smooth line graph isn't plotting my dataset.

My current code is:

PA_county_list %>%
  filter(county %in% c("Chester County","Bucks County")) %>%
  ggplot() +
  geom_smooth(mapping = aes (x = total.pop , y = mean.white, color = county)) +
  labs(title = "Comparing Percent White Race in Chester County and Buck County",
       subtitle = "2010 ACS 5 year census survey",
       y = "White Race Claims",
       x = "Total Population")


This is a sample of the data I am using:

county            total.pop    mean.white            mean.income        per_white
<chr>               <dbl>          <dbl>                 <dbl>             <dbl>
Chester County      41413         3694.957             88997.22           3.716587

Bucks County        47969         3946.140             79940.48           3.969241 

The result of the printed script leads to a labeled blank graph. Where labels are intact but the data from total.pop (population) and mean.white (population of white race) are not listed.

At this point, any insight would be greatly appreciated.

Thanks.


Solution

  • You only have two points in your data judging by your plot title. If that's the case then you wouldn't/couldn't smooth. You can simply connect these dots using geom_line:

    ggplot(df, mapping = aes (x = total.pop , y = mean.white)) +
      geom_point(aes(color = county)) +
      geom_line() +
      labs(title = "Comparing Percent White Race in Chester County and Buck County",
           subtitle = "2010 ACS 5 year census survey",
           y = "White Race Claims",
           x = "Total Population")
    

    enter image description here

    If you had many more data points you could smooth like this:

    ggplot(df, mapping = aes (x = total.pop , y = mean.white)) +
      geom_smooth(method = "loess", formula = y ~ x, color = "black") +
      geom_point(aes(color = county)) 
    

    enter image description here


    Data

    set.seed(1)
    df <- data.frame(county = c("Chester", "Bucks", "Berks", "Montgomery", "Delaware", "Schuylkill"),
                     total.pop = rnorm(6, 48000, 3800)) %>% 
      dplyr::mutate(mean.white = rbeta(6, 5, 2) * total.pop)