From c7cb7c0975d8ca10b5e7c6da982653eedea083df Mon Sep 17 00:00:00 2001 From: gnoblet Date: Wed, 3 May 2023 12:53:31 -0400 Subject: [PATCH] Just for backup / change of laptop --- DESCRIPTION | 2 ++ R/theme_reach.R | 10 ++++++ R/waffle.R | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ README.Rmd | 13 ++++++- 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 R/waffle.R diff --git a/DESCRIPTION b/DESCRIPTION index 54c65ea..f041836 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,5 +29,7 @@ Imports: tidyr, dplyr, ggalluvial +Remotes: + github::hrbrmstr/waffle Suggests: knitr, sf, tmap VignetteBuilder: knitr diff --git a/R/theme_reach.R b/R/theme_reach.R index a05316c..ba20f6d 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -67,6 +67,8 @@ theme_reach <- function( text_color = cols_reach("main_grey"), text_font_face = "plain", panel_background_color = "#FFFFFF", + panel_border = FALSE, + panel_border_color = cols_reach("main_grey"), legend_position = "right", legend_direction = "vertical", legend_reverse = TRUE, @@ -244,6 +246,14 @@ theme_reach <- function( color = grid_minor_color, linewidth = grid_minor_y_size) ) + if (!panel_border) theme_reach <- theme_reach + + ggplot2::theme( + panel.border = ggplot2::element_blank() + ) else theme_reach <- theme_reach + + ggplot2::theme( + panel.border = ggplot2::element_rect(color = panel_background_color) + ) + # Other parameters theme_reach <- theme_reach + ggplot2::theme(...) diff --git a/R/waffle.R b/R/waffle.R new file mode 100644 index 0000000..ae6109a --- /dev/null +++ b/R/waffle.R @@ -0,0 +1,95 @@ +#' @title Simple waffle chart +#' +#' @param df A data frame. +#' @param x A character column or coercible as a character column. Will give the waffle's fill color. +#' @param y A numeric column. +#' @param alpha Fill transparency. +#' @param title Plot title. Default to NULL. +#' @param subtitle Plot subtitle. Default to NULL. +#' @param caption Plot caption. Default to NULL. +#' @param arrange TRUE or FALSE. Arrange by highest percentage first. +#' @param theme Whatever theme. Default to theme_reach(). +#' +#' @return A waffle chart to be used parsimoniously +#' +#' @export +waffle <- function(df, + x, + y, + alpha = 1, + x_title = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + arrange = TRUE, + n_rows = 10, + size = 0.33, + colour = "white", + flip = FALSE, + theme = theme_reach( + + axis_x = FALSE, + axis_y = FALSE)){ + + # Arrange by biggest prop first ? + if (arrange) df <- dplyr::arrange( + df, + dplyr::desc({{ y }}) + ) + + # Get levels for scaling + lev <- dplyr::pull(df, {{ x }}) + df <- dplyr::mutate(df, "{{x}}" := factor({{ x }}, levels = lev)) + + # Mapping + g <- ggplot2::ggplot( + df, + mapping = ggplot2::aes( + fill = {{ x }}, + color = {{ x }}, + values = {{ y }} + ) + ) + + # Add waffle geom + g <- g + + waffle::geom_waffle( + n_rows = n_rows, + size = size, + color = "white", + flip = flip, + alpha = alpha) + + ggplot2::coord_equal() + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + title = title, + subtitle = subtitle, + caption = caption, + fill = x_title, + color = x_title + ) + + # # No axis + # g <- g + ggplot2::theme( + # axis.line.x = ggplot2::element_blank(), + # axis.ticks.x = ggplot2::element_blank(), + # axis.text.x = ggplot2::element_blank(), + # axis.title.x = ggplot2::element_blank(), + # axis.line.y = ggplot2::element_blank(), + # axis.ticks.y = ggplot2::element_blank(), + # axis.text.y = ggplot2::element_blank(), + # axis.title.y = ggplot2::element_blank() + # ) + + # Basic theme + g <- g + + hrbrthemes::theme_ipsum() + + waffle::theme_enhance_waffle() + + # Add theme + g <- g + theme + + return(g) + +} diff --git a/README.Rmd b/README.Rmd index 72f8dd1..b5e4e61 100644 --- a/README.Rmd +++ b/README.Rmd @@ -148,7 +148,7 @@ dumbbell(df, ) ``` -### Example 4: donut chart, REACH themed (to used moderately) +### Example 4: donut chart, REACH themed (to used once, not twice) ```{r example-donut-plot, out.width = "65%", warning = FALSE} # Some summarized data: % of HHs by displacement status @@ -170,6 +170,17 @@ donut(df, theme = theme_reach(legend_reverse = TRUE)) ``` +### Example 4: donut chart, REACH themed (to used once, not twice) +```{r example-donut-plot, out.width = "65%", warning = FALSE} + +# Donut +waffle(df, + status, + percentage, + title = "% of HHs by displacement status", + theme = theme_reach(legend_reverse = TRUE)) +``` + ### Example 5: alluvial chart, REACH themed ```{r example-alluvial-plot, out.width = "65%", warning = FALSE}