vis u ali z ing aspects of data w ith facets
play

Vis u ali z ing aspects of data w ith facets C OMMU N IC ATIN G W - PowerPoint PPT Presentation

Vis u ali z ing aspects of data w ith facets C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE Timo Grossenbacher Data Jo u rnalist The facet _ grid () f u nction ilo_data <- ilo_data %>% filter(year == "1996" | year ==


  1. Vis u ali z ing aspects of data w ith facets C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE Timo Grossenbacher Data Jo u rnalist

  2. The facet _ grid () f u nction ilo_data <- ilo_data %>% filter(year == "1996" | year == "2006") ilo_plot <- ggplot(ilo_data) + geom_histogram(aes( x = working_hours)) + labs(x = "Working hours per week", y = "Number of countries") ilo_plot + facet_grid(. ~ year) ilo_plot + facet_grid(year ~ .) COMMUNICATING WITH DATA IN THE TIDYVERSE

  3. The facet _ grid () f u nction ilo_data <- ilo_data %>% filter(year == "1996" | year == "2006") ggplot(ilo_data) + geom_histogram(aes(x = working_hours)) + labs(x = "Working hours per week", y = "Number of countries") + facet_grid(. ~ year) ggplot(ilo_data) + geom_histogram(aes(x = working_hours)) + labs(x = "Working hours per week", y = "Number of countries") + facet_wrap(facets = ~ year) COMMUNICATING WITH DATA IN THE TIDYVERSE

  4. A faceted scatter plot COMMUNICATING WITH DATA IN THE TIDYVERSE

  5. St y ling faceted plots strip.background strip.text ... COMMUNICATING WITH DATA IN THE TIDYVERSE

  6. Defining y o u r o w n theme f u nction theme_green <- function(){ theme( plot.background = element_rect(fill = "green"), panel.background = element_rect(fill = "lightgreen") )} ggplot(ilo_data) + geom_histogram(aes( x = working_hours)) + labs(x = "Working hours per week", y = "Number of countries") + theme_green() COMMUNICATING WITH DATA IN THE TIDYVERSE

  7. Let ' s practice ! C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE

  8. A c u stom plot to emphasi z e change C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE Timo Grossenbacher Data Jo u rnalist

  9. COMMUNICATING WITH DATA IN THE TIDYVERSE

  10. The dot plot 1 Ne w York Times ( h � ps ://www. n y times . com /2017/11/17/u pshot / income - ineq u alit y-u nited - states . html ){{0}} COMMUNICATING WITH DATA IN THE TIDYVERSE

  11. Dot plots w ith ggplot 2 ggplot((ilo_data %>% filter(year == 2006))) + geom_dotplot(aes(x = working_hours)) + labs(x = "Working hours per week", y = "Share of countries") COMMUNICATING WITH DATA IN THE TIDYVERSE

  12. The geom _ path () f u nction ?geom_path geom_path() connects the obser v ations in the order in w hich the y appear in the data . ilo_data %>% arrange(country) # A tibble: 34 x 4 country year hourly_compensation working_hours <fctr> <fctr> <dbl> <dbl> 1 Austria 1996 24.75 31.99808 2 Austria 2006 30.46 31.81731 3 Belgium 1996 25.25 31.65385 4 Belgium 2006 31.85 30.21154 5 Czech Rep. 1996 2.94 39.72692 # ... with 29 more rows COMMUNICATING WITH DATA IN THE TIDYVERSE

  13. Dot plots w ith ` ggplot 2`: the ` geom _ path ()` f u nction ggplot() + geom_path(aes(x = numeric_variable, y = numeric_variable)) ggplot() + geom_path(aes(x = numeric_variable, y = factor_variable)) ggplot() + geom_path(aes(x = numeric_variable, y = factor_variable), arrow = arrow(___)) COMMUNICATING WITH DATA IN THE TIDYVERSE

  14. Let ' s tr y o u t geom _ path ! C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE

  15. Polishing the dot plot C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE Timo Grossenbacher Data Jo u rnalist

  16. COMMUNICATING WITH DATA IN THE TIDYVERSE

  17. Factor le v els The order of factor le v els determine the order of appearance in ggplot2 . ilo_data$country Austria Belgium Czech Rep. Finland France Germany Hungary ... ... 17 Levels: Austria Belgium Czech Rep. Finland France ... United Kingdom COMMUNICATING WITH DATA IN THE TIDYVERSE

  18. Reordering factors w ith the forcats package Needs to be loaded w ith library(forcats) fct_drop for dropping le v els fct_rev for re v ersing factor le v els fct_reorder for reordering them . 1 Learn more at tid yv erse . org ( h � p :// forcats . tid yv erse . org /) COMMUNICATING WITH DATA IN THE TIDYVERSE

  19. The fct _ reorder f u nction ilo_data # A tibble: 34 x 4 country year hourly_compensation working_hours <fctr> <fctr> <dbl> <dbl> 1 Austria 1996 24.75 31.99808 2 Austria 2006 30.46 31.81731 3 Belgium 1996 25.25 31.65385 4 Belgium 2006 31.85 30.21154 ilo_data <- ilo_data %>% mutate(country = fct_reorder(country, working_hours, mean)) ilo_data$country 17 Levels: Netherlands Norway Germany Sweden ... Czech Rep. COMMUNICATING WITH DATA IN THE TIDYVERSE

  20. COMMUNICATING WITH DATA IN THE TIDYVERSE

  21. N u dging labels w ith hj u st and v j u st ggplot(ilo_data) + geom_path(aes(...)) + geom_text( aes(..., hjust = ifelse(year == "2006", 1.4, -0.4) ) ) COMMUNICATING WITH DATA IN THE TIDYVERSE

  22. Let ' s practice ! C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE

  23. Finali z ing the plot for different a u diences and de v ices C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE Timo Grossenbacher Data Jo u rnalist

  24. COMMUNICATING WITH DATA IN THE TIDYVERSE

  25. coord _ cartesian v s . x lim / y lim ggplot_object + coord_cartesian(xlim = c(0, 100), ylim = c(10, 20)) ggplot_object + xlim(0, 100) + ylim(10, 20) COMMUNICATING WITH DATA IN THE TIDYVERSE

  26. coord _ cartesian v s . x lim / y lim 1 Taken from RSt u dio Data Vis u ali z ation Cheat Sheet ( h � ps :// gith u b . com / rst u dio / cheatsheets / ra w/ master / data -v is u ali z ation -2.1. pdf ) COMMUNICATING WITH DATA IN THE TIDYVERSE

  27. COMMUNICATING WITH DATA IN THE TIDYVERSE

  28. COMMUNICATING WITH DATA IN THE TIDYVERSE

  29. Let ' s prod u ce these plots ! C OMMU N IC ATIN G W ITH DATA IN TH E TIDYVE R SE

Recommend


More recommend