brianhaas19

Example of strip plots for reddit

Oct 26th, 2020 (edited)
917
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(tidyverse)
  2. library(ggbeeswarm)
  3. theme_set(theme_bw())
  4.  
  5. # See post here:
  6. # https://www.reddit.com/r/dataisbeautiful/comments/jidxmm/oc_a_reddit_user_posted_on_rexcel_asking_for_help/ga6aiq1/
  7.  
  8. # Iris data:
  9. p1 <- ggplot(iris) +
  10.   geom_jitter(aes(Species, Sepal.Length),
  11.               width = 0.2,
  12.               color = "steelblue") +
  13.   ggtitle("geom_jitter()")
  14.  
  15. p2 <- ggplot(iris) +
  16.   geom_violin(aes(Species, Sepal.Length),
  17.                 color = "steelblue") +
  18.   ggtitle("geom_violin()")
  19.  
  20. p3 <- ggplot(iris) +
  21.   geom_quasirandom(aes(Species, Sepal.Length),
  22.                    width = 0.2,
  23.                    color = "steelblue") +
  24.   ggtitle("geom_quasirandom()")
  25.  
  26. p1
  27. p2
  28. p3
  29.  
  30. # Make a 1x3 grid:
  31. # library(patchwork)
  32. # P1 <- p1 + p2 + p3
  33. # P1
  34.  
  35. # Rent data:
  36. # (data is in a two column CSV file with col names `price` and `num_bedrooms`)
  37. df <- read_csv("bedrooms_price.csv") %>%
  38.   mutate(num_bedrooms = factor(num_bedrooms),
  39.          num_bedrooms = fct_recode(num_bedrooms,
  40.                                    "Studio" = '0',
  41.                                    "1 BR" = '1',
  42.                                    "2 BR" = '2',
  43.                                    "3 BR" = '3',
  44.                                    "4 BR" = '4',
  45.                                    "5 BR" = '5'))
  46.  
  47. p1 <- ggplot(df) +
  48.   geom_jitter(aes(num_bedrooms, price),
  49.               width = 0.2,
  50.               color = "steelblue") +
  51.   labs(x = "# of Bedrooms", y = "Monthly Rent ($ CAD)",
  52.        title = "geom_jitter()")
  53.  
  54. p2 <- ggplot(df) +
  55.   geom_violin(aes(num_bedrooms, price),
  56.                 color = "steelblue") +
  57.   labs(x = "# of Bedrooms", y = "Monthly Rent ($ CAD)",
  58.        title = "geom_violin()")
  59.  
  60. p3 <- ggplot(df) +
  61.   geom_quasirandom(aes(num_bedrooms, price),
  62.                    width = 0.2,
  63.                    color = "steelblue") +
  64.   labs(x = "# of Bedrooms", y = "Monthly Rent ($ CAD)",
  65.        title = "geom_quasirandom()")
  66.  
  67. p1
  68. p2
  69. p3
  70.  
  71. # Make a 1x3 grid:
  72. # library(patchwork)
  73. # P1 <- p1 + p2 + p3
  74. # P1
RAW Paste Data