From 4fb4ab2b5dea988c63760b22184118e1fb8a4c93 Mon Sep 17 00:00:00 2001 From: gnoblet Date: Mon, 3 Jul 2023 14:38:49 +0200 Subject: [PATCH 1/4] Remove x axis for `donut()` default theme --- R/donut.R | 2 +- README.Rmd | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/donut.R b/R/donut.R index 0c1cd93..268010f 100644 --- a/R/donut.R +++ b/R/donut.R @@ -30,7 +30,7 @@ donut <- function(df, arrange = TRUE, hole_size = 3, add_text = TRUE, - add_text_treshold_display = 5, add_text_color = "white", add_text_suffix = "", theme = theme_reach(legend_reverse = TRUE)){ + add_text_treshold_display = 5, add_text_color = "white", add_text_suffix = "", theme = theme_reach(legend_reverse = TRUE, axis_x = FALSE)){ # Arrange by biggest prop first ? if (arrange) df <- dplyr::arrange( diff --git a/README.Rmd b/README.Rmd index df843f4..110e5d8 100644 --- a/README.Rmd +++ b/README.Rmd @@ -168,7 +168,8 @@ donut(df, add_text_treshold_display = 5, x_title = "Displacement status", title = "% of HHs by displacement status", - theme = theme_reach(legend_reverse = TRUE)) + theme = theme_reach(legend_reverse = TRUE, + axis_x = FALSE)) ``` From a613384bf3c2d2a25c2e0953175de2df25061e1d Mon Sep 17 00:00:00 2001 From: gnoblet Date: Mon, 3 Jul 2023 20:56:54 +0200 Subject: [PATCH 2/4] Threshold and not treshold --- R/donut.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/donut.R b/R/donut.R index 268010f..cd7ff73 100644 --- a/R/donut.R +++ b/R/donut.R @@ -11,7 +11,7 @@ #' @param arrange TRUE or FALSE. Arrange by highest percentage first. #' @param hole_size Hole size. Default to 3. If less than 2, back to a pie chart. #' @param add_text TRUE or FALSE. Add the value as text. -#' @param add_text_treshold_display Minimum value to add the text label. +#' @param add_text_threshold_display Minimum value to add the text label. #' @param add_text_color Text color. #' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label? #' @param theme Whatever theme. Default to theme_reach(). From 5ae7b48626d914541a3dfa850d2600f4fe5b2a37 Mon Sep 17 00:00:00 2001 From: gnoblet Date: Mon, 3 Jul 2023 21:37:53 +0200 Subject: [PATCH 3/4] Treshold to threshold --- R/donut.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/donut.R b/R/donut.R index cd7ff73..626d4d2 100644 --- a/R/donut.R +++ b/R/donut.R @@ -60,7 +60,7 @@ donut <- function(df, # Add text labels if (add_text) { - df <- dplyr::mutate(df, y_treshold = ifelse({{ y }} >= add_text_treshold_display, {{ y }}, NA )) + df <- dplyr::mutate(df, y_treshold = ifelse({{ y }} >= add_text_threshold_display, {{ y }}, NA )) g <- g + ggplot2::geom_text( From a43c2570d5169808bb607b4d90d4405fe6ec85ab Mon Sep 17 00:00:00 2001 From: gnoblet Date: Thu, 13 Jul 2023 17:25:38 +0200 Subject: [PATCH 4/4] Add treemap --- R/treemap.R | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 R/treemap.R diff --git a/R/treemap.R b/R/treemap.R new file mode 100644 index 0000000..bfc8822 --- /dev/null +++ b/R/treemap.R @@ -0,0 +1,79 @@ +#' @title Simple treemap chart +#' +#' @param df A data frame. +#' @param x A character column or coercible as a character column. Will give the treemap's fill color and text. +#' @param y A numeric column of proportions (0 to 100 or 0 to 1). +#' @param tile_border_size Size of the inter-tile space (default to 2). +#' @param tile_start The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. See `treemapify::geom_treemap()`. +#' @param tile_corner_radius The corner radius (defaults to `grid::unit(0, "pt")`). See `treemapify::geom_treemap()`. +#' @param tile_text Boolean. If true, add a text label to each tile (the default). If false, use a side legend only. +#' @param tile_text_size A size (defaults to 20). +#' @param tile_text_color A color (defaults to "white"). +#' @param tile_text_threshold_display Minimum value to add the text label to the tile (defaults to 4). +#' @param tile_text_place Where inside the box to place the text. Default is 'bottom'; other options are 'topleft', 'top', 'topright', etc. See `treemapify::geom_treemap()`. +#' @param x_title The x scale title. Default to NULL. +#' @param title Plot title. Default to NULL. +#' @param subtitle Plot subtitle. Default to NULL. +#' @param caption Plot caption. Default to NULL. +#' @param theme Whatever theme. Default to theme_reach(). +#' +#' @return A waffle chart +#' +#' @export +treemap <- function(df, + x, + y, + tile_border_size = 2, + tile_start = "topleft", + tile_corner_radius = grid::unit(0, "pt"), + tile_text = TRUE, + tile_text_size = 20, + tile_text_color = "white", + tile_text_threshold_display = 4, + tile_text_place = "middle", + x_title = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + theme = theme_reach(reverse = TRUE, panel_border = FALSE, axis_x = FALSE, axis_y = FALSE) + ){ + + # Make plot + g <- ggplot2::ggplot( + data = df, + ggplot2::aes(area = {{ y }}, fill = {{ x }}, label = {{ x }})) + + # Add tile + g <- g + treemapify::geom_treemap( + size = tile_border_size, + radius = tile_corner_radius, + color = "white", + start = tile_start + ) + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + title = title, + subtitle = subtitle, + caption = caption, + fill = x_title, + ) + + # Theme + g <- g + theme + + # If tile_text, show text on tiles and remove legend + if (tile_text) { + g <- g + treemapify::geom_treemap_text( + place = tile_text_place, + start = tile_start, + min.size = tile_text_threshold_display, + color = tile_text_color, + size = tile_text_size + ) + + ggplot2::theme(legend.position = "none") + } + + return(g) + +}