-
-
Save amnek0/f4938fa6ad24a7bccd835c38eef2cd72 to your computer and use it in GitHub Desktop.
The people who keep us company - dataviz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: "R Notebook" | |
| output: html_notebook | |
| --- | |
| ```{r} | |
| library(tidyverse) | |
| # Data from https://www.kaggle.com/bls/american-time-use-survey | |
| df.who <- read_csv('../data/atus/atuswho.csv') %>% | |
| filter(tuwho_code > -0) | |
| df.act <- read_csv('../data/atus/atusact.csv') | |
| df.sum <- read_csv('../data/atus/atussum.csv') | |
| df.resp <- read_csv('../data/atus/atusresp.csv') | |
| df.whocode <- tribble(~tuwho_code, ~who, ~category, | |
| 18, 'Alone', 'Alone', | |
| 19, 'Alone', 'Alone', | |
| 20, 'Spouse', 'Partner', | |
| 21, 'Unmarried partner', 'Partner', | |
| 22, 'Own household child', 'Children', | |
| 23, 'Grandchild', 'Children', | |
| 24, 'Parent', 'Family', | |
| 25, 'Brother/sister', 'Family', | |
| 26, 'Other related person', 'Family', | |
| 27, 'Foster child', 'Children', | |
| 28, 'Housemate/roommate', 'Friend', | |
| 29, 'Roomer/boarder', 'Friend', | |
| 30, 'Other nonrelative', 'Other', | |
| 40, 'Own nonhousehold child < 18', 'Children', | |
| 51, 'Parents (not living in household)', 'Family', | |
| 52, 'Other nonhousehold family members < 18', 'Children', | |
| 53, 'Other nonhousehold family members 18 and older (including parents-in-law)', 'Family', | |
| 54, 'Friends', 'Friend', | |
| 55, 'Co-workers/colleagues/clients', 'Co-worker', | |
| 56, 'Neighbors/acquaintances', 'Other', | |
| 57, 'Other nonhousehold children < 18', 'Other', | |
| 58, 'Other nonhousehold adults 18 and older', 'Other', | |
| 59, 'Boss or manager', 'Co-worker', | |
| 60, 'People whom I supervise', 'Co-worker', | |
| 61, 'Co-workers', 'Co-worker', | |
| 62, 'Co-workers', 'Other', | |
| NA, 'Unknown', 'Unknown' | |
| ) | |
| df.actcode <- read_csv2('../data/atus/codes.csv') %>% | |
| mutate(activity = as.integer(code)) | |
| ``` | |
| ```{r} | |
| df <- df.act %>% | |
| left_join(df.who, by=c('tucaseid', 'tuactivity_n')) %>% | |
| inner_join(df.whocode, by='tuwho_code') | |
| ``` | |
| ```{r} | |
| df.tmp2 <- df %>% | |
| #filter(category != 'Other') %>% | |
| #mutate(category = who) %>% | |
| group_by(category, tucaseid, tuactivity_n) %>% | |
| summarize(tuactdur24 = first(tuactdur24)) %>% | |
| ungroup() %>% | |
| complete(category, tucaseid, fill=list(tuactdur24=0)) %>% | |
| inner_join(df.sum %>% select(tucaseid, teage, tesex, tufnwgtp, tuyear), by='tucaseid') %>% | |
| filter(!((tuyear < 2010) & (category == 'Co-worker'))) %>% | |
| mutate(tesex=1) %>% | |
| group_by(category, tesex, teage, tucaseid) %>% | |
| summarize(tuactdur24 = sum(tuactdur24), | |
| tufnwgtp = first(tufnwgtp)) %>% | |
| summarize(t = sum(tuactdur24 * tufnwgtp) / sum(tufnwgtp)) | |
| df.tmp2 %>% | |
| filter(category != 'Other', category != 'Unknown') %>% | |
| group_by(category) %>% | |
| arrange(teage) %>% | |
| mutate(gender = ifelse(tesex == 1, 'm', 'f'), | |
| category.max = which.max(t)) %>% | |
| ungroup() %>% | |
| mutate(category = factor(category, ordered=TRUE, levels=unique(category[order(category.max)]))) %>% | |
| mutate(category = factor(c)) | |
| ggplot(aes(teage, t/60)) + | |
| geom_line(show.legend = FALSE) + | |
| #geom_bar(stat='identity') + | |
| geom_hline(yintercept=0, size=0.2) + | |
| scale_y_continuous(labels=function(x) {paste0(x, 'h')}) + | |
| labs(x="", y="", title='Who we spend time with', caption='@hnrklndbrg | Source: American Time Use Survey') + | |
| theme_henrik(grid='Y') + | |
| facet_wrap(~ category) | |
| ggsave('/tmp/who.svg', width=7, height=5) | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment