Guest User

Untitled

a guest
May 7th, 2018
39
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---
  2. title: "R Notebook"
  3. output:
  4.   html_document:
  5.     df_print: paged
  6. ---
  7.  
  8. # "Prefer not to answer" in the Developer Survey Results 2018
  9.  
  10.  
  11. ```{r}
  12. library(dplyr)
  13. library(ggplot2)
  14. library(gridExtra)
  15. library(glue)
  16. library(stringi)
  17. ```
  18.  
  19.  
  20. ```{r, fig.width=12}
  21. qualified <- 101592
  22. gender_qualified <- 64364
  23.  
  24. compute_fractions_and_reorder <- function(df, qualified) df %>%
  25.   mutate(fraction = count / qualified) %>%
  26.   mutate(response = factor(response, levels = response[order(fraction)]))
  27.  
  28. plot_bars <- function(df, title) {
  29.   ggplot(df) + geom_col(aes(response, fraction))  +  scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1)) +
  30.     coord_flip() + theme_bw() + ggtitle(title)
  31. }
  32.  
  33. gender <- tibble(
  34.    response =c("Female", "Male",  "Non-binary,\ngenderqueer,\nor gender non-conforming", "Prefer not to answer"),
  35.    count = c(c(0.069,  0.929, 0.009) * gender_qualified, 101592 - gender_qualified)
  36. )
  37. ```
  38.  
  39. ```{r, fig.width=12, fig.height=3}
  40. gender %>% filter(response != "Prefer not to answer") %>% compute_fractions_and_reorder(gender_qualified) %>% plot_bars("Gender")
  41. ```
  42.  
  43. ```{r, fig.width=12, fig.height=4}
  44. gender %>% compute_fractions_and_reorder(qualified) %>% plot_bars("Gender")
  45.  
  46. ```
  47.  
  48.  
  49. ```{r, fig.width=12}
  50.  
  51. race_and_ethnicity_qualified <- 57473
  52.  
  53. race_and_ethnicity <- tibble(
  54.   response = c("White or of European descent", "South Asian",  "Hispanic or Latino/Latina", "East Asian", "Middle Eastern", "Black or of African descent", "Native American,\nPacific Islander,\nor Indigenous Australian", "Prefer not to answer"),
  55.    count = c(c(0.742, 0.115, 0.067, 0.051, 0.041, 0.028, 0.008) * race_and_ethnicity_qualified, qualified - race_and_ethnicity_qualified)
  56. )
  57. ```
  58.  
  59. ```{r, fig.width=12, fig.height=7}
  60. race_and_ethnicity %>% filter(response != "Prefer not to answer") %>% compute_fractions_and_reorder(race_and_ethnicity_qualified) %>% plot_bars("Race and Ethnicity")
  61. ```
  62.  
  63.  
  64. ```{r, fig.width=12, fig.height=8}
  65. race_and_ethnicity %>% compute_fractions_and_reorder(qualified) %>% plot_bars("Race and Ethnicity")
  66. ```
  67.  
  68.  
  69. ```{r, fig.width=12}
  70. sexual_orientation_qualified <- 59765
  71.  
  72. sexual_orientation <- tibble(
  73.   response = c("Straight or heterosexual", "Bisexual or Queer", "Gay or Lesbian", "Asexual", "Prefer not to answer"),
  74.   count = c(c(0.932, 0.043, 0.024, 0.019) * sexual_orientation_qualified, qualified - sexual_orientation_qualified)
  75. )
  76. ```
  77.  
  78. ```{r, fig.width=12, fig.height=4}
  79. sexual_orientation %>% filter(response != "Prefer not to answer") %>% compute_fractions_and_reorder(sexual_orientation_qualified) %>% plot_bars("Sexual orientation")
  80. ```
  81.  
  82.  
  83. ```{r, fig.width=12, fig.height=5}
  84. sexual_orientation %>% compute_fractions_and_reorder(qualified) %>% plot_bars("Sexual orientation")
  85. ```
RAW Paste Data