Site icon R-bloggers

What’s New in CDCPLACES 1.1.5

[This article was first published on Brenden Smith, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

An earlier version of this blog post was published on February 6, 2024 and described the new features in the development version of this package. This update shows all of the new features of the package as of March 16, 2024.

< section id="introduction" class="level2">

Introduction

CDCPLACES version 1.1.5 is now available on CRAN. Users can now request an sf data frame to allow for simple, streamlined mapping of PLACES data. To use this new feature, be sure to install the latest version from CRAN or GitHub.

< details open="" class="code-fold"> < summary>Code
# Install the latest development version
# devtools::install_github("brendensm/CDCPLACES")

# Or from CRAN
# install.packages("CDCPLACES")

library(CDCPLACES)
library(ggplot2)
library(dplyr)
< section id="new-argument-geometry" class="level2">

New argument geometry

First we need to query our data. To include our shape file, we need to specify the argument geometry as “TRUE”. For our first example we will look at the percentage of adults sleeping less than 7 hours in Michigan Counties.

< details open="" class="code-fold"> < summary>Code
mi <- get_places(state = "MI", 
                 measure = "SLEEP", 
                 geometry = TRUE)

Now we can take this dataset and immediately plot the spatial data with ggplot2. I will also add a nicer looking color palette and the percentage scale in scale_fill_viridis_c, as well as a title with the function labs.

< details open="" class="code-fold"> < summary>Code
mi |> 
  filter(datavaluetypeid == "AgeAdjPrv") |> 
  ggplot(aes(fill = data_value)) +
  geom_sf() +
  scale_fill_viridis_c(labels = scales::percent_format(scale = 1)) +
  theme_void() +
  labs(title = mi$measure) +
  theme(plot.title.position = "plot")

We can do the same for census level data. This is as simple as specifying our geography to “census”.

< details open="" class="code-fold"> < summary>Code
vt <- get_places(geography = "census", 
                 state = "VT", 
                 measure = "SLEEP", 
                 geometry = TRUE)

Then we can map it just the same.

< details open="" class="code-fold"> < summary>Code
vt |> 
  ggplot(aes(fill = data_value)) +
  geom_sf() +
  scale_fill_viridis_c(labels = scales::percent_format(scale = 1)) +
  theme_void() +
  labs(title = vt$measure) +
  theme(plot.title.position = "plot")

< section id="query-by-county" class="level2">

Query by County

This update also allows for the user to query specific counties using the argument county. In the example below, we can specify the state and counties we want to plot with simple syntax.

< details open="" class="code-fold"> < summary>Code
cap_county <- get_places(geography = "census", 
                         state = "MI", 
                         measure = "ACCESS2", 
                         county = c("Ingham", "Eaton", "Clinton"), 
                         geometry = TRUE)

Once this is done, we can plot our data.

< details open="" class="code-fold"> < summary>Code
cap_county |> 
  ggplot(aes(fill = data_value)) +
  geom_sf() +
  scale_fill_viridis_c(labels = scales::percent_format(scale = 1)) +
  theme_void() +
  labs(title = cap_county$measure) +
  theme(plot.title.position = "plot")

< section id="acknowledgements" class="level2">

Acknowledgements

These features would not be possible without the excellent work of Kyle Walker’s package tigris. The contributions he has made to the R community have been incredibly inspiring to me. His other package, tidycensus was the inspiration for this entire pacakge. To see his work visit his website here.

To leave a comment for the author, please follow the link and comment on their blog: Brenden Smith.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version