diff --git a/DESCRIPTION b/DESCRIPTION index c1ab644..54c65ea 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: visualizeR Type: Package Title: What a color! What a viz! -Version: 0.6.9000 +Version: 0.7.9000 Authors@R: c( person( 'Noblet', 'Guillaume', @@ -27,6 +27,7 @@ Imports: ggtext, ggrepel, tidyr, - dplyr + dplyr, + ggalluvial Suggests: knitr, sf, tmap VignetteBuilder: knitr diff --git a/NEWS.md b/NEWS.md index 03d4d24..fc1aa03 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,17 @@ # visualizeR 0.6.9000 * Add `dumbbell()`. -* Add further parameters to `theme_reach()` +* Add `alluvial()` +* Add `donut()` +* Add `lollipop()` +* Add further parameters to `theme_reach()`, including grid lines args. + +--- + +# visualizeR 0.6.9000 + +* Add `dumbbell()`. +* Add further parameters to `theme_reach()`. --- diff --git a/R/alluvial.R b/R/alluvial.R new file mode 100644 index 0000000..5665585 --- /dev/null +++ b/R/alluvial.R @@ -0,0 +1,104 @@ +#' @title Simple alluvial chart +#' +#' @param df A data frame. +#' @param from A character column of upstream stratum. +#' @param to A character column of downstream stratum. +#' @param value A numeric column of values. +#' @param group The grouping column to fill the alluvium with. +#' @param alpha Fill transparency. Default to 0.5. +#' @param from_levels Order by given from levels? +#' @param value_title The value/y scale title. Default to NULL. +#' @param group_title The group 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 rect_color Stratum rectangles' fill color. +#' @param rect_border_color Stratum rectangles' border color. +#' @param rect_text_color Stratum rectangles' text color. +#' @param theme Whatever theme. Default to theme_reach(). +#' +#' @return A donut chart to be used parsimoniously +#' +#' @export +alluvial <- function( + df, + from, + to, + value, + group = NULL, + alpha = 0.5, + from_levels = NULL, + value_title = NULL, + group_title = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + rect_color = cols_reach("white"), + rect_border_color = cols_reach("main_grey"), + rect_text_color = cols_reach("main_grey"), + theme = theme_reach(axis_y = FALSE, + legend_position = "none") +){ + + if(!is.null(from_levels)) df <- dplyr::mutate(df, "{{from}}" := factor({{ from }}, levels = from_levels)) + + # General mapping + g <- ggplot2::ggplot( + data = df, + mapping = ggplot2::aes( + y = {{ value }}, + axis1 = {{ from }}, + axis3 = {{ to }} + ) + ) + + # Add alluvium + g <- g + + ggalluvial::geom_alluvium( + ggplot2::aes( + fill = {{ group }}, + color = {{ group }} + ), + alpha = alpha) + + # Add stratum + g <- g + + ggalluvial::geom_stratum( + fill = rect_color, + color = rect_border_color + ) + + # Add stratum text + + stratum <- ggalluvial::StatStratum + + g <- g + + ggplot2::geom_text( + stat = stratum, + ggplot2::aes(label = ggplot2::after_stat(!!rlang::sym("stratum"))), + color = cols_reach("main_grey") + ) + + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + y = value_title, + title = title, + subtitle = subtitle, + caption = caption, + fill = group_title, + color = group_title + ) + + # Remove x-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() + ) + + g <- g + theme + + return(g) +} diff --git a/R/donut.R b/R/donut.R new file mode 100644 index 0000000..3a75efb --- /dev/null +++ b/R/donut.R @@ -0,0 +1,109 @@ +#' @title Simple donut chart (to be used parsimoniously), can be a pie chart +#' +#' @param df A data frame. +#' @param x A character column or coercible as a character column. Will give the donut's fill color. +#' @param y A numeric column. +#' @param alpha Fill transparency. +#' @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 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_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(). +#' +#' @return A donut chart to be used parsimoniously +#' +#' @export +donut <- function(df, + x, + y, + alpha = 1, + x_title = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + 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)){ + + # Arrange by biggest prop first ? + if (arrange) df <- dplyr::arrange( + df, + {{ 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( + x = hole_size, + y = {{ y }}, + fill = {{ x }}, + color = {{ x }} + ) + ) + + # Add rect + g <- g + ggplot2::geom_col(alpha = alpha) + + + # Add text labels + if (add_text) { + + df <- dplyr::mutate(df, y_treshold = ifelse({{ y }} >= add_text_treshold_display, {{ y }}, NA )) + + g <- g + + ggplot2::geom_text( + data = df, + ggplot2::aes( + x = hole_size, + y = !!rlang::sym("y_treshold"), + label = paste0({{ y }}, add_text_suffix)), + color = add_text_color, + position = ggplot2::position_stack(vjust = 0.5)) + } + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + title = title, + subtitle = subtitle, + caption = caption, + fill = x_title, + color = x_title + ) + + # Transform to polar coordinates and adjust hole + g <- g + + ggplot2::coord_polar( + theta = "y" + ) + if (hole_size >= 2) g <- g + ggplot2::xlim(c(1, hole_size + 0.5)) # Try to remove that to see how to make a pie chart + + # 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() + ) + + # Add theme + g <- g + theme + + return(g) + +} diff --git a/R/lollipop.R b/R/lollipop.R new file mode 100644 index 0000000..60de655 --- /dev/null +++ b/R/lollipop.R @@ -0,0 +1,121 @@ +#' @title Simple bar chart +#' +#' @param df A data frame. +#' @param x A numeric column. +#' @param y A character column or coercible as a character column. +#' @param flip TRUE or FALSE. Default to TRUE or horizontal lollipop plot. +#' @param wrap Should x-labels be wrapped? Number of characters. +#' @param arrange TRUE or FALSE. Arrange by highest percentage first. +#' @param point_size Point size. +#' @param point_color Point color. +#' @param point_alpha Point alpha. +#' @param segment_size Segment size. +#' @param segment_color Segment color. +#' @param segment_alpha Segment alpha. +#' @param alpha Fill transparency. +#' @param x_title The x scale title. Default to NULL. +#' @param y_title The y 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 add_text TRUE or FALSE. Add the y value as text within the bubble. +#' @param add_text_size Text size. +#' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label? +#' @param add_text_color Added text color. Default to white. +#' @param add_text_fontface Added text font face. Default to "bold". +#' @param theme Whatever theme. Default to theme_reach(). +#' +#' @return A bar chart +#' +#' @export +lollipop <- function(df, + x, + y, + flip = TRUE, + wrap = NULL, + arrange = TRUE, + point_size = 3, + point_color = cols_reach("main_red"), + point_alpha = 1, + segment_size = 1, + segment_color = cols_reach("main_grey"), + segment_alpha = 1, + alpha = 1, + x_title = NULL, + y_title = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + add_text = FALSE, + add_text_size = 3, + add_text_suffix = "", + add_text_color = "white", + add_text_fontface = "bold", + theme = theme_reach()){ + + + # Arrange by biggest prop first ? + if (arrange) df <- dplyr::arrange( + df, + {{ 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(x = {{ x }}, y = {{ y }}, xend = {{ x }}, yend = 0) + ) + + # Add segment + g <- g + ggplot2::geom_segment( + linewidth = segment_size, + alpha = segment_alpha, + color = segment_color + ) + + g <- g + ggplot2::geom_point( + size = point_size, + alpha = point_alpha, + color = point_color + ) + + if (!is.null(wrap)) { + g <- g + ggplot2::scale_x_discrete(labels = scales::label_wrap(wrap)) + } + + # Because a text legend should always be horizontal, especially for an horizontal bar graph + if (flip){ + g <- g + ggplot2::coord_flip() + } + + # Add text labels + if (add_text) { + g <- g + ggplot2::geom_text( + ggplot2::aes( + label = paste0({{ y }}, add_text_suffix)), + size = add_text_size, + color = add_text_color, + fontface = add_text_fontface) + } + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + title = title, + subtitle = subtitle, + caption = caption, + x = x_title, + y = y_title, + ) + + + # Add theme + g <- g + theme + + return(g) + +} + diff --git a/R/theme_reach.R b/R/theme_reach.R index 0bb1fbe..a05316c 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -29,14 +29,22 @@ #' @param axis_text_size Axis text size. #' @param axis_text_color Axis text color. #' @param axis_text_font_face Axis text font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic"). +#' @param axis_text_x_angle Angle for the x-axis text. +#' @param axis_text_x_vjust Vertical adjustment for the x-axis text. +#' @param axis_text_x_hjust Vertical adjustment for the x-axis text. #' @param axis_title_size Axis title size. #' @param axis_title_color Axis title color. #' @param axis_title_font_face Axis title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic"). -#' @param grid_x Boolean. Do you need major grid lines for x-axis? -#' @param grid_y Boolean. Do you need major grid lines for y-axis? -#' @param grid_x_size X line size. -#' @param grid_y_size Y line size. -#' @param grid_color Grid lines color. +#' @param grid_major_x Boolean. Do you need major grid lines for x-axis? +#' @param grid_major_y Boolean. Do you need major grid lines for y-axis? +#' @param grid_major_x_size Major X line size. +#' @param grid_major_y_size Major Y line size. +#' @param grid_major_color Major grid lines color. +#' @param grid_minor_x Boolean. Do you need minor grid lines for x-axis? +#' @param grid_minor_y Boolean. Do you need minor grid lines for y-axis? +#' @param grid_minor_x_size Minor X line size. +#' @param grid_minor_y_size Minor Y line size. +#' @param grid_minor_color Minor grid lines color. #' @param caption_position_to_plot TRUE or FALSE. Positioning to plot or to panel? #' @param ... Additional arguments passed to `ggplot2::gg_theme()`. #' @@ -50,7 +58,7 @@ theme_reach <- function( palette = "main", discrete = TRUE, reverse = FALSE, - font_family = "Leelawadee", + font_family = "Segoe UI", title_size = 12, title_color = cols_reach("main_grey"), title_font_face = "bold", @@ -76,11 +84,19 @@ theme_reach <- function( axis_title_size = 11, axis_title_color = cols_reach("main_grey"), axis_title_font_face = "bold", - grid_x = FALSE, - grid_y = FALSE, - grid_color = cols_reach("main_lt_grey"), - grid_x_size = 0.1, - grid_y_size = 0.1, + axis_text_x_angle = 0, + axis_text_x_vjust = 0.5, + axis_text_x_hjust = 0.5, + grid_major_x = FALSE, + grid_major_y = FALSE, + grid_major_color = cols_reach("main_lt_grey"), + grid_major_x_size = 0.1, + grid_major_y_size = 0.1, + grid_minor_x = FALSE, + grid_minor_y = FALSE, + grid_minor_color = cols_reach("main_lt_grey"), + grid_minor_x_size = 0.05, + grid_minor_y_size = 0.05, caption_position_to_plot = TRUE, ... ) { @@ -114,9 +130,6 @@ theme_reach <- function( panel.background = ggplot2::element_rect( fill = panel_background_color ), - # Remove minor grid lines - panel.grid.minor.x = ggplot2::element_blank(), - panel.grid.minor.y = ggplot2::element_blank(), # Remove background for legend key legend.key = ggplot2::element_blank(), # Text sizes @@ -145,6 +158,11 @@ theme_reach <- function( face = legend_text_font_face, family = font_family, color = legend_text_color + ), + axis.text.x = ggplot2::element_text( + angle = axis_text_x_angle, + vjust = axis_text_x_vjust, + hjust = axis_text_x_hjust ) ) @@ -184,27 +202,49 @@ theme_reach <- function( } # X - major grid lines - if (!grid_x) theme_reach <- theme_reach + + if (!grid_major_x) theme_reach <- theme_reach + ggplot2::theme( panel.grid.major.x = ggplot2::element_blank() ) else theme_reach <- theme_reach + ggplot2::theme( panel.grid.major.x = ggplot2::element_line( - color = grid_color, - linewidth = grid_y_size) + color = grid_major_color, + linewidth = grid_major_x_size) ) # Y - major grid lines - if (!grid_y) theme_reach <- theme_reach + + if (!grid_major_y) theme_reach <- theme_reach + ggplot2::theme( panel.grid.major.y = ggplot2::element_blank() ) else theme_reach <- theme_reach + ggplot2::theme( panel.grid.major.y = ggplot2::element_line( - color = grid_color, - linewidth = grid_y_size) + color = grid_major_color, + linewidth = grid_major_y_size) ) + # X - minor grid lines + if (!grid_minor_x) theme_reach <- theme_reach + + ggplot2::theme( + panel.grid.minor.x = ggplot2::element_blank() + ) else theme_reach <- theme_reach + + ggplot2::theme( + panel.grid.minor.x = ggplot2::element_line( + color = grid_minor_color, + linewidth = grid_minor_x_size) + ) + + # Y - minor grid lines + if (!grid_minor_y) theme_reach <- theme_reach + + ggplot2::theme( + panel.grid.minor.y = ggplot2::element_blank() + ) else theme_reach <- theme_reach + + ggplot2::theme( + panel.grid.minor.y = ggplot2::element_line( + color = grid_minor_color, + linewidth = grid_minor_y_size) + ) + # Other parameters theme_reach <- theme_reach + ggplot2::theme(...) diff --git a/README.Rmd b/README.Rmd index 8d4e0a5..72f8dd1 100644 --- a/README.Rmd +++ b/README.Rmd @@ -43,10 +43,11 @@ Roadmap is as follows: - [X] Add IMPACT's colors - [X] Add all color palettes from the internal documentation - [ ] There remains to be added more-than-7-color palettes and black color palettes -- [ ] Add new types of visualization (e.g. dumbbell plot) -- [ ] Use examples +- [X] Add new types of visualization (e.g. dumbbell plot, lollipop plot, etc.) +- [X] Use examples - [ ] Add some ease-map functions - [ ] Add some interactive functions (maps and graphs) +- [ ] Consolidate and make errors transparent ## Request @@ -73,7 +74,7 @@ pal_reach(show_palettes = T) ### Example 1: Bar chart, already REACH themed -```{r example-bar-chart, eval = TRUE} +```{r example-bar-chart, out.width = "65%", eval = TRUE} library(visualizeR) library(palmerpenguins) library(dplyr) @@ -100,7 +101,7 @@ bar(df, island, mean_bl, species, group_title = "Species", flip = FALSE, add_tex At this stage, `point_reach()` only supports categorical grouping colors with the `group` arg. -```{r example-point-chart, eval = TRUE} +```{r example-point-chart, out.width = "65%", eval = TRUE} # Simple point chart point(penguins, bill_length_mm, flipper_length_mm) @@ -109,39 +110,143 @@ point(penguins, bill_length_mm, flipper_length_mm) point(penguins, bill_length_mm, flipper_length_mm, island, alpha = 0.6, size = 3, theme = theme_reach(reverse = TRUE)) # Using another color palettes -point(penguins, bill_length_mm, flipper_length_mm, island, size = 1.5, x_title = "Bill", y_title = "Flipper", title = "Length (mm)", theme = theme_reach(palette = "artichoke_3", text_font_face = , grid_x = T, title_position_to_plot = FALSE)) +point(penguins, bill_length_mm, flipper_length_mm, island, size = 1.5, x_title = "Bill", y_title = "Flipper", title = "Length (mm)", theme = theme_reach(palette = "artichoke_3", text_font_face = , grid_major_x = TRUE, title_position_to_plot = FALSE)) ``` -### Example 3! Dumbbell plot, REACH themed +### Example 3: Dumbbell plot, REACH themed Remember to ensure that your data are in the long format and you only have two groups on the x-axis; for instance, IDP and returnee and no NA values. -```{r example-dumbbell-plot, eval = TRUE} +```{r example-dumbbell-plot, out.width = "65%", eval = TRUE} # Prepare long data df <- tibble::tibble( - admin1 = rep(c("A", "B", "C", "D", "E", "F", "G", "H"), 2), + admin1 = rep(letters[1:8], 2), setting = c(rep(c("Rural", "Urban"), 4), rep(c("Urban", "Rural"), 4)), stat = rnorm(16, mean = 50, sd = 18) ) |> dplyr::mutate(stat = round(stat, 0)) -# Example +# Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title dumbbell(df, stat, setting, admin1, title = "% of HHs that reported open defecation as sanitation facility", group_y_title = "Admin 1", + group_x_title = "Setting", theme = theme_reach(legend_position = "bottom", legend_direction = "horizontal", + legend_title_font_face = "bold", palette = "primary", - title_position_to_plot = FALSE)) + title_position_to_plot = FALSE, + legend.title.align = 0.5)) + + # Change legend title position (could be included as part of the function) + ggplot2::guides( + color = ggplot2::guide_legend(title.position = "left"), + fill = ggplot2::guide_legend(title.position = "left") + ) ``` +### Example 4: donut chart, REACH themed (to used moderately) +```{r example-donut-plot, out.width = "65%", warning = FALSE} + +# Some summarized data: % of HHs by displacement status +df <- tibble::tibble( + status = c("Displaced", "Non displaced", "Returnee", "Don't know/Prefer not to say"), + percentage = c(18, 65, 12, 3) +) + +# Donut +donut(df, + status, + percentage, + hole_size = 3, + add_text_suffix = "%", + add_text_color = cols_reach("dk_grey"), + add_text_treshold_display = 5, + x_title = "Displacement status", + 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} + +# Some summarized data: % of HHs by self-reported status of displacement in 2021 and in 2022 +df <- tibble::tibble( + status_from = c(rep("Displaced", 4), + rep("Non displaced", 4), + rep("Returnee", 4), + rep("Dnk/Pnts", 4)), + status_to = c("Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts"), + percentage = c(20, 8, 18, 1, 12, 21, 0, 2, 0, 3, 12, 1, 0, 0, 1, 1) +) + +# Alluvial, here the group is the status for 2021 + +alluvial(df, + status_from, + status_to, + percentage, + status_from, + from_levels = c("Displaced", "Non displaced", "Returnee", "Dnk/Pnts"), + alpha = 0.8, + group_title = "Status for 2021", + title = "% of HHs by self-reported status from 2021 to 2022", + theme = theme_reach( + axis_y = FALSE, + legend_position = "none")) + +``` + +### Example 6: lollipop chart +```{r example-lollipop-chart, out.width = "65%", warning = FALSE} +library(tidyr) +# Prepare long data +df <- tibble::tibble( + admin1 = replicate(15, sample(letters, 8)) |> t() |> as.data.frame() |> unite("admin1", sep = "") |> dplyr::pull(admin1), + stat = rnorm(15, mean = 50, sd = 15)) |> + dplyr::mutate(stat = round(stat, 0)) + +# Make lollipop plot, REACH themed, vertical with 45 degrees angle X-labels +lollipop(df, + admin1, + stat, + arrange = FALSE, + add_text = FALSE, + flip = FALSE, + y_title = "% of HHs", + x_title = "Admin 1", + title = "% of HHs that reported having received a humanitarian assistance", + theme = theme_reach(axis_text_x_angle = 45, + grid_major_y = TRUE, + grid_major_y_size = 0.2, + grid_major_x = TRUE, + grid_minor_y = TRUE)) + +# Horizontal, greater point size, arranged by value, no grid, and text labels added +lollipop(df, + admin1, + stat, + arrange = TRUE, + point_size = 10, + point_color = cols_reach("main_beige"), + segment_size = 2, + add_text = TRUE, + add_text_suffix = "%", + y_title = "% of HHs", + x_title = "Admin 1", + title = "% of HHs that reported having received a humanitarian assistance in the 12 months prior to the assessment", + theme = theme_reach(title_position_to_plot = FALSE)) + + +``` + + ## Maps -```{r example-map} +```{r example-map, out.width = "50%"} # Add indicator layer # - based on "pretty" classes and title "Proportion (%)" diff --git a/README.md b/README.md index 1b1d0be..c2e6914 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,12 @@ Roadmap is as follows: - [x] Add all color palettes from the internal documentation - [ ] There remains to be added more-than-7-color palettes and black color palettes -- [ ] Add new types of visualization (e.g. dumbbell plot) -- [ ] Use examples +- [x] Add new types of visualization (e.g. dumbbell plot, lollipop + plot, etc.) +- [x] Use examples - [ ] Add some ease-map functions - [ ] Add some interactive functions (maps and graphs) +- [ ] Consolidate and make errors transparent ## Request @@ -93,21 +95,21 @@ df <- penguins |> bar(df, island, mean_bl, species, percent = FALSE, alpha = 0.6, x_title = "Mean of bill length") ``` - + ``` r # Using another color palette through `theme_reach()` and changing scale to percent bar(df, island,mean_bl, species, percent = TRUE, theme = theme_reach(palette = "artichoke_3")) ``` - + ``` r # Not flipped, with text added, group_title, no y-axis and no bold for legend bar(df, island, mean_bl, species, group_title = "Species", flip = FALSE, add_text = TRUE, add_text_suffix = "%", percent = FALSE, theme = theme_reach(text_font_face = "plain", axis_y = FALSE)) ``` - + ### Example 2: Point chart, already REACH themed @@ -119,23 +121,23 @@ with the `group` arg. point(penguins, bill_length_mm, flipper_length_mm) ``` - + ``` r # Point chart with grouping colors, greater dot size, some transparency, reversed color palette point(penguins, bill_length_mm, flipper_length_mm, island, alpha = 0.6, size = 3, theme = theme_reach(reverse = TRUE)) ``` - + ``` r # Using another color palettes -point(penguins, bill_length_mm, flipper_length_mm, island, size = 1.5, x_title = "Bill", y_title = "Flipper", title = "Length (mm)", theme = theme_reach(palette = "artichoke_3", text_font_face = , grid_x = T, title_position_to_plot = FALSE)) +point(penguins, bill_length_mm, flipper_length_mm, island, size = 1.5, x_title = "Bill", y_title = "Flipper", title = "Length (mm)", theme = theme_reach(palette = "artichoke_3", text_font_face = , grid_major_x = TRUE, title_position_to_plot = FALSE)) ``` - + -### Example 3! Dumbbell plot, REACH themed +### Example 3: Dumbbell plot, REACH themed Remember to ensure that your data are in the long format and you only have two groups on the x-axis; for instance, IDP and returnee and no NA @@ -144,26 +146,137 @@ values. ``` r # Prepare long data df <- tibble::tibble( - admin1 = rep(c("A", "B", "C", "D", "E", "F", "G", "H"), 2), + admin1 = rep(letters[1:8], 2), setting = c(rep(c("Rural", "Urban"), 4), rep(c("Urban", "Rural"), 4)), stat = rnorm(16, mean = 50, sd = 18) ) |> dplyr::mutate(stat = round(stat, 0)) -# Example +# Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title dumbbell(df, stat, setting, admin1, title = "% of HHs that reported open defecation as sanitation facility", group_y_title = "Admin 1", + group_x_title = "Setting", theme = theme_reach(legend_position = "bottom", legend_direction = "horizontal", + legend_title_font_face = "bold", palette = "primary", - title_position_to_plot = FALSE)) + title_position_to_plot = FALSE, + legend.title.align = 0.5)) + + # Change legend title position (could be included as part of the function) + ggplot2::guides( + color = ggplot2::guide_legend(title.position = "left"), + fill = ggplot2::guide_legend(title.position = "left") + ) ``` - + + +### Example 4: donut chart, REACH themed (to used moderately) + +``` r +# Some summarized data: % of HHs by displacement status +df <- tibble::tibble( + status = c("Displaced", "Non displaced", "Returnee", "Don't know/Prefer not to say"), + percentage = c(18, 65, 12, 3) +) + +# Donut +donut(df, + status, + percentage, + hole_size = 3, + add_text_suffix = "%", + add_text_color = cols_reach("dk_grey"), + add_text_treshold_display = 5, + x_title = "Displacement status", + title = "% of HHs by displacement status", + theme = theme_reach(legend_reverse = TRUE)) +``` + + + +### Example 5: alluvial chart, REACH themed + +``` r +# Some summarized data: % of HHs by self-reported status of displacement in 2021 and in 2022 +df <- tibble::tibble( + status_from = c(rep("Displaced", 4), + rep("Non displaced", 4), + rep("Returnee", 4), + rep("Dnk/Pnts", 4)), + status_to = c("Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts", "Displaced", "Non displaced", "Returnee", "Dnk/Pnts"), + percentage = c(20, 8, 18, 1, 12, 21, 0, 2, 0, 3, 12, 1, 0, 0, 1, 1) +) + +# Alluvial, here the group is the status for 2021 + +alluvial(df, + status_from, + status_to, + percentage, + status_from, + from_levels = c("Displaced", "Non displaced", "Returnee", "Dnk/Pnts"), + alpha = 0.8, + group_title = "Status for 2021", + title = "% of HHs by self-reported status from 2021 to 2022", + theme = theme_reach( + axis_y = FALSE, + legend_position = "none")) +``` + + + +### Example 6: lollipop chart + +``` r +library(tidyr) +# Prepare long data +df <- tibble::tibble( + admin1 = replicate(15, sample(letters, 8)) |> t() |> as.data.frame() |> unite("admin1", sep = "") |> dplyr::pull(admin1), + stat = rnorm(15, mean = 50, sd = 15)) |> + dplyr::mutate(stat = round(stat, 0)) + +# Make lollipop plot, REACH themed, vertical with 45 degrees angle X-labels +lollipop(df, + admin1, + stat, + arrange = FALSE, + add_text = FALSE, + flip = FALSE, + y_title = "% of HHs", + x_title = "Admin 1", + title = "% of HHs that reported having received a humanitarian assistance", + theme = theme_reach(axis_text_x_angle = 45, + grid_major_y = TRUE, + grid_major_y_size = 0.2, + grid_major_x = TRUE, + grid_minor_y = TRUE)) +``` + + + +``` r +# Horizontal, greater point size, arranged by value, no grid, and text labels added +lollipop(df, + admin1, + stat, + arrange = TRUE, + point_size = 10, + point_color = cols_reach("main_beige"), + segment_size = 2, + add_text = TRUE, + add_text_suffix = "%", + y_title = "% of HHs", + x_title = "Admin 1", + title = "% of HHs that reported having received a humanitarian assistance in the 12 months prior to the assessment", + theme = theme_reach(title_position_to_plot = FALSE)) +``` + + ## Maps diff --git a/docs/404.html b/docs/404.html index b23886a..9c5ece6 100644 --- a/docs/404.html +++ b/docs/404.html @@ -31,7 +31,7 @@ visualizeR - 0.6.9000 + 0.7.9000 + + + + + +
+
+
+ +
+

Simple alluvial chart

+
+ +
+

Usage

+
alluvial(
+  df,
+  from,
+  to,
+  value,
+  group = NULL,
+  alpha = 0.5,
+  from_levels = NULL,
+  value_title = NULL,
+  group_title = NULL,
+  title = NULL,
+  subtitle = NULL,
+  caption = NULL,
+  rect_color = cols_reach("white"),
+  rect_border_color = cols_reach("main_grey"),
+  rect_text_color = cols_reach("main_grey"),
+  theme = theme_reach(axis_y = FALSE, legend_position = "none")
+)
+
+ +
+

Arguments

+
df
+

A data frame.

+ + +
from
+

A character column of upstream stratum.

+ + +
to
+

A character column of downstream stratum.

+ + +
value
+

A numeric column of values.

+ + +
group
+

The grouping column to fill the alluvium with.

+ + +
alpha
+

Fill transparency. Default to 0.5.

+ + +
from_levels
+

Order by given from levels?

+ + +
value_title
+

The value/y scale title. Default to NULL.

+ + +
group_title
+

The group title. Default to NULL.

+ + +
title
+

Plot title. Default to NULL.

+ + +
subtitle
+

Plot subtitle. Default to NULL.

+ + +
caption
+

Plot caption. Default to NULL.

+ + +
rect_color
+

Stratum rectangles' fill color.

+ + +
rect_border_color
+

Stratum rectangles' border color.

+ + +
rect_text_color
+

Stratum rectangles' text color.

+ + +
theme
+

Whatever theme. Default to theme_reach().

+ +
+
+

Value

+ + +

A donut chart to be used parsimoniously

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/bar.html b/docs/reference/bar.html index 4d2c736..e21989c 100644 --- a/docs/reference/bar.html +++ b/docs/reference/bar.html @@ -10,7 +10,7 @@ visualizeR - 0.6.9000 + 0.7.9000 + + + + + +
+
+
+ +
+

Simple donut chart (to be used parsimoniously), can be a pie chart

+
+ +
+

Usage

+
donut(
+  df,
+  x,
+  y,
+  alpha = 1,
+  x_title = NULL,
+  title = NULL,
+  subtitle = NULL,
+  caption = NULL,
+  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)
+)
+
+ +
+

Arguments

+
df
+

A data frame.

+ + +
x
+

A character column or coercible as a character column. Will give the donut's fill color.

+ + +
y
+

A numeric column.

+ + +
alpha
+

Fill transparency.

+ + +
x_title
+

The x scale title. Default to NULL.

+ + +
title
+

Plot title. Default to NULL.

+ + +
subtitle
+

Plot subtitle. Default to NULL.

+ + +
caption
+

Plot caption. Default to NULL.

+ + +
arrange
+

TRUE or FALSE. Arrange by highest percentage first.

+ + +
hole_size
+

Hole size. Default to 3. If less than 2, back to a pie chart.

+ + +
add_text
+

TRUE or FALSE. Add the value as text.

+ + +
add_text_treshold_display
+

Minimum value to add the text label.

+ + +
add_text_color
+

Text color.

+ + +
add_text_suffix
+

If percent is FALSE, should we add a suffix to the text label?

+ + +
theme
+

Whatever theme. Default to theme_reach().

+ +
+
+

Value

+ + +

A donut chart to be used parsimoniously

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/dumbbell.html b/docs/reference/dumbbell.html index 574f907..59d7e15 100644 --- a/docs/reference/dumbbell.html +++ b/docs/reference/dumbbell.html @@ -10,7 +10,7 @@ visualizeR - 0.6.9000 + 0.7.9000 + + + + + +
+
+
+ +
+

Simple bar chart

+
+ +
+

Usage

+
lollipop(
+  df,
+  x,
+  y,
+  flip = TRUE,
+  wrap = NULL,
+  arrange = TRUE,
+  point_size = 3,
+  point_color = cols_reach("main_red"),
+  point_alpha = 1,
+  segment_size = 1,
+  segment_color = cols_reach("main_grey"),
+  segment_alpha = 1,
+  alpha = 1,
+  x_title = NULL,
+  y_title = NULL,
+  title = NULL,
+  subtitle = NULL,
+  caption = NULL,
+  add_text = FALSE,
+  add_text_size = 3,
+  add_text_suffix = "",
+  add_text_color = "white",
+  add_text_fontface = "bold",
+  theme = theme_reach()
+)
+
+ +
+

Arguments

+
df
+

A data frame.

+ + +
x
+

A numeric column.

+ + +
y
+

A character column or coercible as a character column.

+ + +
flip
+

TRUE or FALSE. Default to TRUE or horizontal lollipop plot.

+ + +
wrap
+

Should x-labels be wrapped? Number of characters.

+ + +
arrange
+

TRUE or FALSE. Arrange by highest percentage first.

+ + +
point_size
+

Point size.

+ + +
point_color
+

Point color.

+ + +
point_alpha
+

Point alpha.

+ + +
segment_size
+

Segment size.

+ + +
segment_color
+

Segment color.

+ + +
segment_alpha
+

Segment alpha.

+ + +
alpha
+

Fill transparency.

+ + +
x_title
+

The x scale title. Default to NULL.

+ + +
y_title
+

The y scale title. Default to NULL.

+ + +
title
+

Plot title. Default to NULL.

+ + +
subtitle
+

Plot subtitle. Default to NULL.

+ + +
caption
+

Plot caption. Default to NULL.

+ + +
add_text
+

TRUE or FALSE. Add the y value as text within the bubble.

+ + +
add_text_size
+

Text size.

+ + +
add_text_suffix
+

If percent is FALSE, should we add a suffix to the text label?

+ + +
add_text_color
+

Added text color. Default to white.

+ + +
add_text_fontface
+

Added text font face. Default to "bold".

+ + +
theme
+

Whatever theme. Default to theme_reach().

+ +
+
+

Value

+ + +

A bar chart

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/pal_agora.html b/docs/reference/pal_agora.html index 7afeb80..50111bc 100644 --- a/docs/reference/pal_agora.html +++ b/docs/reference/pal_agora.html @@ -10,7 +10,7 @@ visualizeR - 0.6.9000 + 0.7.9000