Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make function histogram_facet_plot() #56

Closed
spsanderson opened this issue Sep 3, 2021 · 1 comment
Closed

Make function histogram_facet_plot() #56

spsanderson opened this issue Sep 3, 2021 · 1 comment
Assignees
Labels
function A new function

Comments

@spsanderson
Copy link
Owner

No description provided.

@spsanderson spsanderson added the function A new function label Sep 3, 2021
@spsanderson spsanderson self-assigned this Sep 3, 2021
@spsanderson spsanderson added this to the healthyR.ai 0.0.3 milestone Sep 3, 2021
@spsanderson
Copy link
Owner Author

spsanderson commented Sep 3, 2021

histogram_facet_plot <- function(
  .data
  , .bins = 10
  , .ncol = 5
  , .fct_reorder = FALSE
  , .fct_rev = FALSE
  , .fill = tidyquant::palette_light()[[3]]
  , .color = "white"
  , .scale = "free"
) {
  data_factored <- data %>%
    mutate_if(is.character, as.factor) %>%
    mutate_if(is.factor, as.numeric) %>%
    gather(key = key, value = value, factor_key = TRUE)

if (fct_reorder) {
  data_factored <- data_factored %>%
    mutate(key = as.character(key) as.factor())
  }

if (fct_rev){
  data_factored <- data_factored %>%
    mutate(key = fct_rev(key))
  }

g <- data_factored %>%
  ggplot(aes(x = value, group = key)) +
  geom_histogram(bins = bins, fill = fill, color = color) +
  facet_wrap(~ key, ncol = ncol, scale = scale) +
  theme_tq()

return(g)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function A new function
Development

No branches or pull requests

1 participant