Introduction

Census data is readily available for many populations related studies, it is free of charge and easy to download. This week we learn about census data related package in R and get to test it out in this report.

Data Preparation

For data download and preparation, I installed tidycensus package, especially its get_acs() function, it not only can access non-spatial data, it can also request spatial data. It is very convenient. After you load the data, you can further manipulate data from there use dplyr package. Such as filter or grouping etc.

Analysis

List of package I used for this report:

#loading packages for the project
library(tidycensus)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(scales)
library(stringr)
library(plotly)

Part A of this report will generate an interactive graph.

As California is one of my favourite states in US. I choose “CA” as my study state. First, I started by requesting data from census.

#Part A

#request data from census database on population percentage for people who have a graduate degree by county.(2021)
graduate_pop <- get_acs(
  geography = "county", 
  variables = "DP02_0066P", 
  state = "CA", #<<
  year = 2021
)

Then I was able to find the highest and lowest percentage state by using filtering founcation.

#find out the lowest percentage
lowest_graduate <- graduate_pop%>%
  filter(estimate== min(estimate,na.rm = TRUE))

Error can be determined by the difference between estimate and “moe”. Therefore we found out the minimum and maximum error value. And a plot is created.

#create a plot for population with graduate degree
error_plot<-ggplot(graduate_pop, aes(x = estimate, y = reorder(NAME, estimate))) + 
  geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
                width = 0.5, linewidth = 0.5) +
  geom_point(color = "orange", size = 2) + 
  scale_x_continuous() + 
  scale_y_discrete(labels = function(x) str_remove(x, " County, California|, California")) + 
  labs(title = "Percentage of the Population That Have a Graduate Degree, 2017-2021 ACS",
       subtitle = "Counties in California",
       caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
       x = "ACS estimate",
       y = "") 

Converting it to an interactive plot. You can click it and find out the values.

#make the plot interactive
ggplotly(error_plot, tooltip = "x")

Part B of this report will generate a choropleth map

I am very interested in population working from home after COVID, 2019.

#load package
library(mapview)

#discovery variable of census 
#vars <- load_variables(2021, "acs5")
#View(vars)


#decided to study work from home population with geometry
wfh_pop <- get_acs(
  geography = "county", 
  variables = "B08522_025", 
  state = "WA", 
  year = 2021,
  geometry = TRUE
)
##   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |==============                                                        |  19%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  38%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |========================================================              |  81%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |==========================================================            |  84%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
#take a look at data spatially 

mapview(wfh_pop, zcol = "estimate")

I am surprised to see only three states that have WFH population greater than 50000, and only one greater than 200000.

#creating the static map of population work from home
ggplot(wfh_pop, aes(fill = estimate)) + 
  geom_sf() + 
  scale_color_brewer(palette = "Set1") +  
  theme_void() + 
  labs(title = "Population That Work From Home, 2021 ACS",
       subtitle = "Counties in Washington",
       caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
       x = "ACS estimate",
       y = "") 

# Conclusion

In conclusion, I think the three counties that has highest population work from home may because they has have the most dense population among all other counties. Therefore, we cannot rely on one single graph to understand a situation. We should study one topic in may perspective and make an education conclusion