diff --git a/06_working_continuous.Rmd b/06_working_continuous.Rmd index 4493c04..4a497d5 100644 --- a/06_working_continuous.Rmd +++ b/06_working_continuous.Rmd @@ -594,10 +594,11 @@ africa2002 <- gapdata %>% # save as africa2002 head(africa2002) # inspect africa2002 %>% - gather(key, lifeExp, -country) %>% # gather values to same column - ggplot(aes(x = lifeExp)) + + # pivot lifeExp and lifeExp_log values to same column (for easy plotting): + pivot_longer(contains("lifeExp")) %>% + ggplot(aes(x = value)) + geom_histogram(bins = 15) + # make histogram - facet_wrap(~key, scales = "free") # facet with axes free to vary + facet_wrap(~name, scales = "free") # facet with axes free to vary ``` diff --git a/09_logistic_regression.Rmd b/09_logistic_regression.Rmd index 5dd4164..8583754 100644 --- a/09_logistic_regression.Rmd +++ b/09_logistic_regression.Rmd @@ -422,7 +422,7 @@ melanoma %>% mort_5yr.num = as.numeric(mort_5yr) - 1 ) %>% select(mort_5yr.num, age, year) %>% - gather(key = "predictors", value = "value", -mort_5yr.num) %>% + pivot_longer(all_of(c("age", "year")), names_to = "predictors") %>% ggplot(aes(x = value, y = mort_5yr.num)) + geom_point(size = 0.5, alpha = 0.5) + geom_smooth(method = "loess") + @@ -509,11 +509,11 @@ select_explanatory <- c("ulcer.factor", "sex.factor", "t_stage.factor") melanoma %>% select(one_of(select_explanatory)) %>% - gather(key, value, -sex.factor) %>% + pivot_longer(-sex.factor) %>% ggplot(aes(value, fill = sex.factor)) + geom_bar(position = "fill") + ylab("proportion") + - facet_wrap(~key, scale = "free", ncol = 2) + + facet_wrap(~name, scale = "free", ncol = 2) + coord_flip() ``` diff --git a/data/gbd_process.R b/data/gbd_process.R index 9f57cb5..d91355b 100644 --- a/data/gbd_process.R +++ b/data/gbd_process.R @@ -28,7 +28,7 @@ gbd_short %>% gbd_long %>% write_csv(path = "data/global_burden_disease_cause-year-sex-income.csv") -#Chapter 3 - spread() and gather() +#Chapter 3 - pivot_wider() and pivot_longer() gbd_long_example = gbd_long %>% filter(year %in% c(1990, 2017)) %>% group_by(cause, sex, year) %>%