Guest User

Untitled

a guest
Feb 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. # Packages
  2. ```{r}
  3. library(dplyr)
  4. library(ggplot2)
  5. ```
  6.  
  7. # Normal plot for one year
  8.  
  9. ```{r}
  10. social_care_df %>%
  11. filter(pack_length != 0 &
  12. year_1112 == TRUE) %>%
  13. ggplot(aes(age_group, pack_length, fill = census_1112) +
  14. geom_boxplot() +
  15. coord_flip() +
  16. lots of lines of plot options
  17. ```
  18. # As a function
  19.  
  20. ```{r}
  21. plot_function <- function(df, year, census){
  22.  
  23. year <- enquo(year)
  24.  
  25. df %>%
  26. filter(pack_lenght != 0 &
  27. (!!year) == TRUE) %>% #Bang bang for tidyeval
  28. ggplot(aes_(quote(age_group), quote(pack_length), fill = as.name(census))) + #aes_ , quote(.) and as.name for NSE
  29. geom_boxplot() +
  30. coord_flip() +
  31. lots of lines of plot options
  32. ```
  33.  
  34. # To run on a different year
  35.  
  36. ```{r}
  37. plot_function(social_care_df, year_1213, "census_1213") # ggplot argument needs to be quoted
  38. ```
  39.  
  40. Note - `age_group` and `pack_length` are columns passed in dataframe NOT function arguments
Add Comment
Please, Sign In to add comment