the facets layer
play

The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 - PowerPoint PPT Presentation

The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy Facets Straight-forward yet useful Concept of Small Multiples Popularized by Edward Tufte Visualization of Quantitative Information,


  1. The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy

  2. Facets Straight-forward yet useful Concept of Small Multiples Popularized by Edward Tufte Visualization of Quantitative Information, 1983 INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  3. INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  4. INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  5. iris.wide p <- ggplot(iris.wide, aes(x = L y = W ccolo geom_jitter(alpha = 0.7) + scale_color_brewer(palette = " coord_fixed() p INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  6. iris.wide & facet_grid() p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) + geom_jitter(alpha = 0.7) + scale_color_brewer(palette = "Set1") + coord_fixed() p + facet_grid(cols = vars(Species)) INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  7. Formula notation p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) + geom_jitter(alpha = 0.7) + scale_color_brewer(palette = "Set1") + coord_fixed() p + facet_grid(. ~ Species) INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  8. iris.wide2 ggplot(iris.wide2, aes(x = Part, y = setosa, color = Measure)) + geom_jitter() ggplot(iris.wide2, aes(x = Part, y = versicolor, color = Measure)) + geom_jitter() ggplot(iris.wide2, aes(x = Part, y = virginica, color = Measure)) + geom_jitter() INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  9. iris.tidy ggplot(iris.tidy, aes(x = Measure, y = Value, color = Part)) + geom_jitter() + facet_grid(cols = vars(Species)) INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  10. iris.tidy faceting done wrong: ggplot(iris.tidy, aes(x = Me y = Va color geom_jitter() + facet_grid(rows = vars(Spe INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  11. Other options Split according to rows and columns INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  12. Let's practice! IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

  13. Facet labels and order IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy

  14. A new dataframe # Plot p <- ggplot(msleep2, aes(bod bra geom_point(alpha = 0.6, sh coord_fixed() p INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  15. A new dataframe, with facets p + facet_grid(rows = vars(vor INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  16. A new dataframe, with facets p + facet_grid(rows = vars(vor INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  17. Poor labels and order p + facet_grid(rows = vars(vor Two typical problems with facets: Poorly labeled (e.g. non descriptive) Wrong or inappropriate order INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  18. Poor labels and order p + facet_grid(rows = vars(vor Solutions: Easy: Add labels in ggplot Better: Relabel and rearrange factor variables in your dataframe INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  19. The labeller argument # Default is to label the va p + facet_grid(rows = vars(vor labeller = labe INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  20. Using label_both adds the variable name # Print variable name also p + facet_grid(rows = vars(vor labeller = la INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  21. Two variables on one side p + facet_grid(rows = vars(vor con INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  22. Using label_context avoids ambiguity p + facet_grid(rows = vars(vor con labeller = la INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  23. Use rows and columns when appropriate p + facet_grid(rows = vars(vor cols = vars(con labeller = labe INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  24. Use rows and columns when appropriate p + facet_grid(rows = vars(vor cols = vars(con INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  25. Use rows and columns when appropriate INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  26. Relabeling and reordering factors msleep2$conservation <- fct_recode(msleep2$conservation, Domesticated = "domestic `Least concern` = "lc", `Near threatened` = "nt" Vulnerable = "vu", Endangered = "en") msleep2$vore = fct_recode(msleep2$vore, Carnivore = "carni", Herbivore = "herbi", Insectivore = "insecti", Omnivore = "omni") INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  27. Reinitialize plot with new labels # Plot p <- ggplot(msleep2, aes(bod bra geom_point(alpha = 0.6, sh coord_fixed() p + facet_grid(rows = vars(vor cols = vars(con INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  28. Reinitialize plot with new labels INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  29. Changing the order of levels # Change order of levels: msleep2$conservation = fct_relevel(msleep2$conservation, c("Domesticated", "Least concern", "Near threatened", "Vulnerable", "Endangered")) INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  30. Reinitialize plot with new order INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  31. Let's practice! IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

  32. Facet plotting spaces IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy

  33. Facets and variable plotting spaces Reasons to not use consistent plotting spaces: Variable type Subsets contains Continuous Wildly different ranges Categorical Different groups INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  34. Adjusting the plotting space... ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh coord_fixed() + facet_grid(rows = vars(vor cols = vars(con INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  35. ... but not with �xed scales ggplot(msleep2, aes(bodywt_log, brainwt_log)) + geom_point(alpha = 0.6, shape = 16) + coord_fixed() + facet_grid(rows = vars(vore), cols = vars(conservation), scales = "free_x") Error: coord_fixed doesn't support free scales INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  36. Adjusting the plotting space ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free_ INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  37. Adjusting the plotting space ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free_ INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  38. Adjusting the plotting space ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free" INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  39. Adjusting the plotting space ggplot(msleep2, aes(x = body y = name geom_point() + facet_grid(rows = vars(vor INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  40. Adjusting the plotting space ggplot(msleep2, aes(x = body y = name geom_point() + # Free the y scales and sp facet_grid(rows = vars(vor scales = "free_ INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  41. Adjusting the plotting space ggplot(msleep2, aes(x = body y = name geom_point() + # Free the y scales and sp facet_grid(rows = vars(vor scales = "free_ space = "free_y INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  42. Final adjustments msleep2 <- msleep2 %>% # Arrange from lo to hi weight arrange(-bodywt_log) %>% # Redefine factor levels in or mutate(name = as_factor(name)) # New order is reflected in y ax ggplot(msleep2, aes(x = bodywt_l y = name)) + geom_point() + # Free the y scales and space facet_grid(rows = vars(vore), scales = "free_y", space = "free_y") INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  43. Let's practice! IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

  44. Facet wrap & margins IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy

  45. Adjusting the plotting space ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free" INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  46. Using facet_wrap() Use cases: 1. When you want both x and y axes to be free on every individual plot i.e. Not just per row or column as per facet_grid() INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  47. Using facet_wrap() - Scenario 1 ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_wrap(vars(vore, cons scales = "free" INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  48. Using facet_wrap() Use cases: 1. When you want both x and y axes to be free on every individual plot i.e. Not just per row or column as per facet_grid() 2. When your categorical (factor) variable has many groups (levels) i.e. too many sub plots for column or row-wise faceting A more typical scenario INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  49. Using facet_wrap() - Scenario 2 ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_wrap(vars(order)) INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

  50. Using margin plots ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free" INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Recommend


More recommend