Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Packages
- ```{r}
- library(dplyr)
- library(ggplot2)
- ```
- # Normal plot for one year
- ```{r}
- social_care_df %>%
- filter(pack_length != 0 &
- year_1112 == TRUE) %>%
- ggplot(aes(age_group, pack_length, fill = census_1112) +
- geom_boxplot() +
- coord_flip() +
- lots of lines of plot options
- ```
- # As a function
- ```{r}
- plot_function <- function(df, year, census){
- year <- enquo(year)
- df %>%
- filter(pack_lenght != 0 &
- (!!year) == TRUE) %>% #Bang bang for tidyeval
- ggplot(aes_(quote(age_group), quote(pack_length), fill = as.name(census))) + #aes_ , quote(.) and as.name for NSE
- geom_boxplot() +
- coord_flip() +
- lots of lines of plot options
- ```
- # To run on a different year
- ```{r}
- plot_function(social_care_df, year_1213, "census_1213") # ggplot argument needs to be quoted
- ```
- Note - `age_group` and `pack_length` are columns passed in dataframe NOT function arguments
Add Comment
Please, Sign In to add comment