Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Created December 1, 2017 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/afa459a28818f36e6f38b8bea95df18e to your computer and use it in GitHub Desktop.
Save cpsievert/afa459a28818f36e6f38b8bea95df18e to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
library(readr)
# For other measures, see http://ghdx.healthdata.org/us-data
# TODO: do this on the county level for more interesting data, like this post - https://fivethirtyeight.com/features/mortality-black-belt/
obesity <- read_csv("http://ghdx.healthdata.org/sites/default/files/record-attached-files/IHME_USA_OBESITY_PHYSICAL_ACTIVITY_2001_2011.csv")
# reshape data so value tracks
ob <- obesity %>%
gather(variable, value, matches("prevalence [0-9]+ \\(%\\)")) %>%
mutate(year = stringr::str_extract(variable, "[0-9]+"))
ob_sum <- ob %>%
rename(name = State) %>%
group_by(name, year) %>%
summarise(value = mean(value, na.rm = TRUE))
d <- ob_sum %>%
left_join(us_lcc) %>%
st_as_sf()
p <- ggplot(d) +
geom_sf(aes(fill = value, frame = year)) +
ggthemes::theme_map()
gg <- plotly_build(p)
# TODO: perhaps I need to report this as a bug to plotly.js?
# https://github.com/plotly/plotly.js/issues/1721
gg$x$frames <- lapply(
gg$x$frames, function(f) {
f$data <- lapply(f$data, function(d) d[!names(d) %in% c("x", "y")])
f
})
gg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment