21 add treemap plot
This commit is contained in:
Guillaume NOBLET 2023-07-14 11:13:44 +02:00 committed by GitHub
commit df519888ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 4 deletions

View file

@ -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().
@ -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(
@ -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(

79
R/treemap.R Normal file
View file

@ -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)
}

View file

@ -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))
```