From 0c4b9a8c1181d38a297231eae3d21286e7452657 Mon Sep 17 00:00:00 2001
From: Kyle Walker Basic usage of tidycensus
load the package along with the tidyverse package, and
set their Census API key. A key can be obtained from http://api.census.gov/data/key_signup.html.
-library(tidycensus)
-library(tidyverse)
-
-census_api_key("YOUR API KEY GOES HERE")
library(tidycensus)
+library(tidyverse)
+
+census_api_key("YOUR API KEY GOES HERE")
There are two major functions implemented in
tidycensus: get_decennial()
, which grants
access to the 2000, 2010, and 2020 decennial US Census APIs, and
@@ -130,21 +130,21 @@
-age20 <- get_decennial(geography = "state",
- variables = "P13_001N",
- year = 2020,
- sumfile = "dhc")
-
-head(age20)
## # A tibble: 6 × 4
-## GEOID NAME variable value
-## <chr> <chr> <chr> <dbl>
-## 1 09 Connecticut P13_001N 41.1
-## 2 10 Delaware P13_001N 41.1
-## 3 11 District of Columbia P13_001N 33.9
-## 4 12 Florida P13_001N 43
-## 5 13 Georgia P13_001N 37.5
-## 6 15 Hawaii P13_001N 40.8
+age20 <- get_decennial(geography = "state",
+ variables = "P13_001N",
+ year = 2020,
+ sumfile = "dhc")
+
+head(age20)
+## # A tibble: 6 × 4
+## GEOID NAME variable value
+## <chr> <chr> <chr> <dbl>
+## 1 09 Connecticut P13_001N 41.1
+## 2 10 Delaware P13_001N 41.1
+## 3 11 District of Columbia P13_001N 33.9
+## 4 12 Florida P13_001N 43
+## 5 13 Georgia P13_001N 37.5
+## 6 15 Hawaii P13_001N 40.8
The function returns a tibble with four columns by default:
GEOID
, which is an identifier for the geographical unit
associated with the row; NAME
, which is a descriptive name
@@ -158,9 +158,9 @@
As the function has returned a tidy object, we can visualize it quickly with ggplot2:
-age20 %>%
- ggplot(aes(x = value, y = reorder(NAME, value))) +
- geom_point()
age20 %>%
+ ggplot(aes(x = value, y = reorder(NAME, value))) +
+ geom_point()
-v17 <- load_variables(2017, "acs5", cache = TRUE)
-
-View(v17)
v17 <- load_variables(2017, "acs5", cache = TRUE)
+
+View(v17)
By filtering for “median age” variable IDs corresponding to that query can be browsed interactively. For the 5-year ACS detailed tables @@ -480,29 +480,29 @@
-vt <- get_acs(geography = "county",
- variables = c(medincome = "B19013_001"),
- state = "VT",
- year = 2021)
-
-vt
## # A tibble: 14 × 5
-## GEOID NAME variable estimate moe
-## <chr> <chr> <chr> <dbl> <dbl>
-## 1 50001 Addison County, Vermont medincome 77978 3393
-## 2 50003 Bennington County, Vermont medincome 63448 3413
-## 3 50005 Caledonia County, Vermont medincome 55159 3974
-## 4 50007 Chittenden County, Vermont medincome 81957 2521
-## 5 50009 Essex County, Vermont medincome 48194 3577
-## 6 50011 Franklin County, Vermont medincome 68476 3297
-## 7 50013 Grand Isle County, Vermont medincome 85154 7894
-## 8 50015 Lamoille County, Vermont medincome 66016 4777
-## 9 50017 Orange County, Vermont medincome 67906 2710
-## 10 50019 Orleans County, Vermont medincome 58037 3153
-## 11 50021 Rutland County, Vermont medincome 59751 2133
-## 12 50023 Washington County, Vermont medincome 70128 3014
-## 13 50025 Windham County, Vermont medincome 59195 2060
-## 14 50027 Windsor County, Vermont medincome 63787 2209
+vt <- get_acs(geography = "county",
+ variables = c(medincome = "B19013_001"),
+ state = "VT",
+ year = 2021)
+
+vt
+## # A tibble: 14 × 5
+## GEOID NAME variable estimate moe
+## <chr> <chr> <chr> <dbl> <dbl>
+## 1 50001 Addison County, Vermont medincome 77978 3393
+## 2 50003 Bennington County, Vermont medincome 63448 3413
+## 3 50005 Caledonia County, Vermont medincome 55159 3974
+## 4 50007 Chittenden County, Vermont medincome 81957 2521
+## 5 50009 Essex County, Vermont medincome 48194 3577
+## 6 50011 Franklin County, Vermont medincome 68476 3297
+## 7 50013 Grand Isle County, Vermont medincome 85154 7894
+## 8 50015 Lamoille County, Vermont medincome 66016 4777
+## 9 50017 Orange County, Vermont medincome 67906 2710
+## 10 50019 Orleans County, Vermont medincome 58037 3153
+## 11 50021 Rutland County, Vermont medincome 59751 2133
+## 12 50023 Washington County, Vermont medincome 70128 3014
+## 13 50025 Windham County, Vermont medincome 59195 2060
+## 14 50027 Windsor County, Vermont medincome 63787 2209
The output is similar to a call to get_decennial()
, but
instead of a value
column, get_acs
returns
estimate
and moe
columns for the ACS estimate
@@ -513,15 +513,15 @@
-vt %>%
- mutate(NAME = gsub(" County, Vermont", "", NAME)) %>%
- ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
- geom_errorbarh(aes(xmin = estimate - moe, xmax = estimate + moe)) +
- geom_point(color = "red", size = 3) +
- labs(title = "Household income by county in Vermont",
- subtitle = "2017-2021 American Community Survey",
- y = "",
- x = "ACS estimate (bars represent margin of error)")
vt %>%
+ mutate(NAME = gsub(" County, Vermont", "", NAME)) %>%
+ ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
+ geom_errorbarh(aes(xmin = estimate - moe, xmax = estimate + moe)) +
+ geom_point(color = "red", size = 3) +
+ labs(title = "Household income by county in Vermont",
+ subtitle = "2017-2021 American Community Survey",
+ y = "",
+ x = "ACS estimate (bars represent margin of error)")
diff --git a/docs/articles/basic-usage_files/figure-html/unnamed-chunk-4-1.png b/docs/articles/basic-usage_files/figure-html/unnamed-chunk-4-1.png
index 1e2b0e83e0c7bb74c76999cfcf29a2bb6b4cac5b..3af9d96f94ec4a9adb165307705ed748ac0defe3 100644
GIT binary patch
literal 169343
zcmd432RPRK|2BR}(U7J>Mv1aY85tQ3l@*c^84aU|?7g}hT2dF1%tH31lAV<#TO!$n
z*XWE@dvVU4fv|gH<`@L3b`{`Yy_k{8u;+!B${-h2i{=L=Bn*m*7)#T RK4lXLfa@GXq4MStT5clWZ`iP5rdMu}4`Q00
z{riw#aya0D_LpORVPrZsHa2%}-+owlj(0r3(?K9>eFKuE`A_Mk^L*&!HV~e{x1&|7
zXVWa3wZZOEZr+}wT}3(+6rP gY9x{vioL`dCtF~{ua
zWV4?CX)OV?wL?-e7_$9K5KUy&C+L!+kAl0D_{efcfGdIl=WCYT?pU>mT{z1N^Rsri
zoTnB>N;RL|TwLQfHj-?MS#F)8njvVRHL7e+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