diff --git a/DESCRIPTION b/DESCRIPTION index 54c65ea..2fed535 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: visualizeR Type: Package Title: What a color! What a viz! -Version: 0.7.9000 +Version: 0.8.9000 Authors@R: c( person( 'Noblet', 'Guillaume', @@ -20,7 +20,7 @@ LazyData: true RoxygenNote: 7.2.3 Imports: ggplot2, - rlang, + rlang (>= 0.4.11), grDevices, glue, scales, @@ -28,6 +28,12 @@ Imports: ggrepel, tidyr, dplyr, - ggalluvial -Suggests: knitr, sf, tmap + ggalluvial, + viridisLite, + waffle +Suggests: + knitr, + roxygen2, + sf, + tmap VignetteBuilder: knitr diff --git a/NEWS.md b/NEWS.md index fc1aa03..61ad5bf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,13 @@ -# visualizeR 0.6.9000 +# visualizeR 0.8.9000 + +* Add `waffle()`. +* Add a `pal_default()` function as a fallback for scale functions and as an initiative in `theme_reach()`. It uses `viridis::magma()`. +* Update of AGORA palettes. +* Some other small bug fixes. + +--- + +# visualizeR 0.7.9000 * Add `dumbbell()`. * Add `alluvial()` diff --git a/R/donut.R b/R/donut.R index 3a75efb..0c1cd93 100644 --- a/R/donut.R +++ b/R/donut.R @@ -87,23 +87,21 @@ donut <- function(df, 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() - ) + 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 # Add theme g <- g + theme + # No axis + g <- g + ggplot2::theme( + axis.text = ggplot2::element_blank(), + axis.line = ggplot2::element_blank(), + axis.ticks = ggplot2::element_blank(), + axis.title = ggplot2::element_blank() + ) + + return(g) } diff --git a/R/pal_agora.R b/R/pal_agora.R index 90f08a6..756e9d8 100644 --- a/R/pal_agora.R +++ b/R/pal_agora.R @@ -13,15 +13,9 @@ pal_agora <- function(palette = "main", reverse = FALSE, color_ramp_palette = FA palettes_agora <- list( - `main` = cols_agora("main_grey", "main_red", "main_lt_grey", "main_beige"), - `primary` = cols_agora("main_grey", "main_red"), - `secondary` = cols_agora("main_lt_grey", "main_beige"), - `two_dots` = cols_agora("two_dots_1", "two_dots_2"), - `two_dots_flashy` = cols_agora("two_dots_flashy_1", "two_dots_flashy_2"), - `red_main` = cols_agora("red_main_1", "red_main_2", "red_main_3", "red_main_4", "red_main_5"), - `red_alt` = cols_agora("red_alt_1", "red_alt_2", "red_alt_3", "red_alt_4", "red_alt_5"), - `iroise` = cols_agora("iroise_1", "iroise_2", "iroise_3", "iroise_4", "iroise_5"), - `discrete_6` = cols_agora("dk_grey", "red_main_1", "main_beige", "red_main_2", "lt_grey_2", "red_4") + `main` = cols_agora("main_bordeaux", "main_dk_beige", "main_lt_grey", "main_lt_beige"), + `primary` = cols_agora("main_bordeaux", "main_dk_beige"), + `secondary` = cols_agora( "main_lt_grey", "main_lt_beige") ) if (show_palettes) return(names(palettes_agora)) diff --git a/R/pal_fallback.R b/R/pal_fallback.R new file mode 100644 index 0000000..0fb7b23 --- /dev/null +++ b/R/pal_fallback.R @@ -0,0 +1,30 @@ +#' @title Return function to interpolate a fallback palette base on viridis::magma() +#' +#' @param reverse Boolean indicating whether the palette should be reversed +#' @param color_ramp_palette Should the output be a `grDevices::colorRampPalette` function or a vector of hex codes? Default to the latter with `FALSE` +#' @param discrete Boolean. Discrete or not? Default to FALSE. +#' @param n Number of colors in the palette. Default to 5. Passe to `viridis::magma()` +#' @param ... Other parameters to pass to `grDevices::colorRampPalette()` +#' +#' @return A color palette +#' +#' @export +pal_fallback <- function(reverse = FALSE, + color_ramp_palette = FALSE, + discrete = FALSE, + n = 5, + ...){ + + pal <- if(discrete) { viridisLite::viridis(n) } else {viridisLite::magma(n)} + + if (reverse) pal <- rev(pal) + + if (color_ramp_palette) { + rlang::check_installed("grDevices", reason = "Package \"grDevices\" needed for `pal_fallback()` with 'color_ramp_palette' set to `TRUE` to work. Please install it.") + + pal <- grDevices::colorRampPalette(pal, ...) + } + + return(pal) + +} diff --git a/R/scale.R b/R/scale.R index 8dec956..b1a8ccd 100644 --- a/R/scale.R +++ b/R/scale.R @@ -1,6 +1,6 @@ #' Color scale constructor for REACH or AGORA colors #' -#' @param initiative Either "reach" or "agora. +#' @param initiative Either "reach" or "agora" or "default". #' @param palette Palette name from `pal_reach()` or `pal_agora()`. #' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param reverse Boolean indicating whether the palette should be reversed. @@ -11,24 +11,85 @@ #' @return A color scale for ggplot #' #' @export -scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, reverse_guide = TRUE, ...) { +scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, reverse_guide = TRUE, ...) { if (initiative == "reach") { - pal <- pal_reach( - palette = palette, - reverse = reverse, - color_ramp_palette = TRUE, - show_palettes = FALSE + + pal <- pal_reach(palette) + + if (is.null(pal)) { + + pal <- pal_fallback( + reverse = reverse, + discrete = discrete, + color_ramp_palette = TRUE) + + rlang::warn( + c( + paste0("There is no palette '", palette, "' for the selected initiative. Fallback to pal_fallback()."), + "i" = paste0("Use `pal_reach(show_palettes = TRUE)` to see the list of available palettes.") + ) ) + + if (discrete) palette <- "viridis" else palette <- "magma" + + } else { + + pal <- pal_reach( + palette = palette, + reverse = reverse, + color_ramp_palette = TRUE, + show_palettes = FALSE + ) + + } + } else if (initiative == "agora") { - pal <- pal_agora( - palette = palette, + + pal <- pal_agora(palette) + + if (is.null(pal)) { + + pal <- pal_fallback( + reverse = reverse, + discrete = discrete, + color_ramp_palette = TRUE) + + rlang::warn( + c( + paste0("There is no palette '", palette, "' for the selected initiative. Fallback to pal_fallback()."), + "i" = paste0("Use `pal_reach(show_palettes = TRUE)` to see the list of available palettes.") + ) + ) + + if (discrete) palette <- "viridis" else palette <- "magma" + + } else { + + pal <- pal_agora( + palette = palette, + reverse = reverse, + color_ramp_palette = TRUE, + show_palettes = FALSE + ) + } + + } else if (initiative == "default") { + + pal <- pal_fallback( reverse = reverse, - color_ramp_palette = TRUE, - show_palettes = FALSE - ) + discrete = discrete, + color_ramp_palette = TRUE) + + if (discrete) palette <- "viridis" else palette <- "magma" + } else { - rlang::abort(c("Wrong initiative parameter input", "*" = paste0(initiative, "is not an option"), "i" = "Parameter 'initiative' should be one of 'reach' or 'agora'")) + rlang::abort( + c( + paste0("There is no initiative '", initiative, "."), + "i" = paste0("initiative should be either 'reach', 'agora' or 'default'") + ) + ) } if (discrete) { @@ -41,8 +102,10 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE draw.ulim = TRUE, draw.llim = TRUE, ticks.colour = "#F1F3F5", - reverse = reverse_guide), - ...) + reverse = reverse_guide + ), + ... + ) } else { ggplot2::scale_color_gradientn( colours = pal(256), @@ -53,7 +116,8 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE ticks.colour = "#F1F3F5", reverse = reverse_guide ), - ...) + ... + ) } } @@ -61,7 +125,7 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE #' Fill scale constructor for REACH or AGORA colors #' -#' @param initiative Either "reach" or "agora. +#' @param initiative Either "reach" or "agora" or "default". #' @param palette Palette name from `pal_reach()` or `pal_agora()`. #' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param reverse Boolean indicating whether the palette should be reversed. @@ -72,24 +136,86 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE #' @return A fill scale for ggplot. #' #' @export -scale_fill <- function(initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, reverse_guide = TRUE, ...) { +scale_fill <- function(initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, reverse_guide = TRUE, ...) { + if (initiative == "reach") { - pal <- pal_reach( - palette = palette, - reverse = reverse, - color_ramp_palette = TRUE, - show_palettes = FALSE - ) + + pal <- pal_reach(palette) + + if (is.null(pal)) { + + pal <- pal_fallback( + reverse = reverse, + discrete = discrete, + color_ramp_palette = TRUE) + + rlang::warn( + c( + paste0("There is no palette '", palette, "' for the selected initiative. Fallback to pal_fallback()."), + "i" = paste0("Use `pal_reach(show_palettes = TRUE)` to see the list of available palettes.") + ) + ) + + if (discrete) palette <- "viridis" else palette <- "magma" + + } else { + + pal <- pal_reach( + palette = palette, + reverse = reverse, + color_ramp_palette = TRUE, + show_palettes = FALSE + ) + + } + } else if (initiative == "agora") { - pal <- pal_agora( - palette = palette, + + pal <- pal_agora(palette) + + if (is.null(pal)) { + + pal <- pal_fallback( + reverse = reverse, + discrete = discrete, + color_ramp_palette = TRUE) + + rlang::warn( + c( + paste0("There is no palette '", palette, "' for the selected initiative. Fallback to pal_fallback()."), + "i" = paste0("Use `pal_reach(show_palettes = TRUE)` to see the list of available palettes.") + ) + ) + + if (discrete) palette <- "viridis" else palette <- "magma" + + } else { + + pal <- pal_agora( + palette = palette, + reverse = reverse, + color_ramp_palette = TRUE, + show_palettes = FALSE + ) + } + + } else if (initiative == "default") { + + pal <- pal_fallback( reverse = reverse, - color_ramp_palette = TRUE, - show_palettes = FALSE - ) + discrete = discrete, + color_ramp_palette = TRUE) + + if (discrete) palette <- "viridis" else palette <- "magma" + } else { - rlang::abort(c("Wrong initiative parameter input", "*" = paste0(initiative, "is not an option"), "i" = "Parameter 'initiative' should be one of 'reach' or 'agora'")) + rlang::abort( + c( + paste0("There is no initiative '", initiative, "."), + "i" = paste0("initiative should be either 'reach', 'agora' or 'default'") + ) + ) } if (discrete) { @@ -102,17 +228,21 @@ scale_fill <- function(initiative = "reach", palette = "main", discrete = TRUE, draw.ulim = TRUE, draw.llim = TRUE, ticks.colour = "#F1F3F5", - reverse = reverse_guide), - ...) + reverse = reverse_guide + ), + ... + ) } else { - ggplot2::scale_fill_gradientn( + ggplot2::scale_color_gradientn( colours = pal(256), guide = ggplot2::guide_colorbar( title.position = "top", draw.ulim = TRUE, draw.llim = TRUE, ticks.colour = "#F1F3F5", - reverse = reverse_guide), - ...) + reverse = reverse_guide + ), + ... + ) } } diff --git a/R/theme_reach.R b/R/theme_reach.R index a05316c..ac4a3be 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -1,16 +1,20 @@ #' @title ggplot2 theme with REACH color palettes #' +#' @param initiative Either "reach" or "default". #' @param palette Palette name from 'pal_reach()'. #' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param reverse Boolean indicating whether the palette should be reversed. -#' @param font_family The font family for all plot's texts. Default to "Leelawadee". +#' @param font_family The font family for all plot's texts. Default to "Segoe UI". #' @param title_size The size of the title. Defaults to 12. #' @param title_color Title color. #' @param title_font_face Title font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic"). +#' @param title_hjust Title horizontal justification. Default to NULL. Use 0.5 to center the title. #' @param text_size The size of all text other than the title, subtitle and caption. Defaults to 10. #' @param text_color Text color. #' @param text_font_face Text font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic"). #' @param panel_background_color The color for the panel background color. Default to white. +#' @param panel_border Boolean. Plot a panel border? Default to FALSE. +#' @param panel_border_color A color. Default to REACH main grey. #' @param legend_position Position of the legend; Default to "right". Can take "right", "left", "top", "bottom" or "none". #' @param legend_direction Direction of the legend. Default to "vertical". Can take "vertical" or "horizontal". #' @param legend_title_size Legend title size. @@ -55,6 +59,7 @@ #' #' @export theme_reach <- function( + initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, @@ -62,11 +67,14 @@ theme_reach <- function( title_size = 12, title_color = cols_reach("main_grey"), title_font_face = "bold", + title_hjust = NULL, title_position_to_plot = TRUE, text_size = 10, 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, @@ -104,6 +112,14 @@ theme_reach <- function( # To do : # - add facet theming + if (!initiative %in% c("reach", "default")) + rlang::abort( + c( + paste0("There is no initiative '", initiative, " to be used with theme_reach()."), + "i" = paste0("initiative should be either 'reach' or 'default'") + ) + ) + # Basic simple theme # theme_reach <- ggplot2::theme_bw() @@ -145,9 +161,13 @@ theme_reach <- function( face = axis_title_font_face, color = axis_title_color), # Wrap title - plot.title = ggtext::element_textbox_simple(), - plot.subtitle = ggtext::element_textbox_simple(), - plot.caption = ggtext::element_textbox_simple(), + plot.title = ggtext::element_textbox( + hjust = title_hjust + ), + plot.subtitle = ggtext::element_textbox( + hjust = title_hjust + ), + plot.caption = ggtext::element_textbox(), legend.title = ggplot2::element_text( size = legend_title_size, face = legend_title_font_face, @@ -244,32 +264,24 @@ 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(...) - - # Check if palette is an actual existing palette - pal <- pal_reach(palette) - - if(is.null(pal)) { - rlang::warn( - c( - paste0("There is no palette '", palette, "' for initiative 'reach'. Fallback to REACH main palette."), - "i" = paste0("Use `pal_reach(show_palettes = TRUE)` to see the list of availabale palettes.") - ) - ) - - palette <- "main" - - } - # Add reach color palettes by default # (reversed guide is defaulted to TRUE for natural reading) theme_reach <- list( theme_reach, - scale_color(palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse), - scale_fill(palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse) + scale_color(initiative = initiative, palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse), + scale_fill(initiative = initiative, palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse) ) diff --git a/R/visualizeR-package.R b/R/visualizeR-package.R new file mode 100644 index 0000000..62800aa --- /dev/null +++ b/R/visualizeR-package.R @@ -0,0 +1,7 @@ +#' @keywords internal +"_PACKAGE" + +## usethis namespace: start +#' @importFrom rlang := +## usethis namespace: end +NULL diff --git a/R/waffle.R b/R/waffle.R new file mode 100644 index 0000000..7df06d9 --- /dev/null +++ b/R/waffle.R @@ -0,0 +1,74 @@ +#' @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 (if plotting proportion, make sure to have percentages between 0 and 100 and not 0 and 1). +#' @param n_rows Number of rows. Default to 10. +#' @param size Width of the separator between blocks (defaults to 2). +#' @param x_title The x scale title. Default to NULL. +#' @param x_lab The x scale caption. 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 theme Whatever theme. Default to theme_reach(). +#' +#' @return A waffle chart +#' +#' @export +waffle <- function(df, + x, + y, + n_rows = 10, + size = 2, + x_title = NULL, + x_lab = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + arrange = TRUE, + theme = theme_reach( + axis_x = FALSE, + axis_y = FALSE, + legend_position = "bottom", + legend_direction = "horizontal", + title_hjust = 0.5)){ + + # A basic and not robust check + # - add check between 0 and 1 + + # Arrange by biggest prop first ? + if (arrange) df <- dplyr::arrange( + df, + dplyr::desc({{ y }}) + ) + + # Mutate to 100 + # df <- dplyr::mutate(df, "{{y}}" := {{ y }} * 100) + + # Prepare named vector + values <- stats::setNames(dplyr::pull(df, {{ y }}), dplyr::pull(df, {{ x }})) + + # Make plot + g <- waffle::waffle(values, xlab = x_lab, rows = n_rows, size = size) + + # Add title, subtitle, caption, x_title, y_title + g <- g + ggplot2::labs( + title = title, + subtitle = subtitle, + caption = caption, + fill = x_title, + color = x_title + ) + + # 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..df843f4 100644 --- a/README.Rmd +++ b/README.Rmd @@ -128,6 +128,7 @@ df <- tibble::tibble( dplyr::mutate(stat = round(stat, 0)) # Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title + dumbbell(df, stat, setting, @@ -148,7 +149,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,7 +171,15 @@ donut(df, theme = theme_reach(legend_reverse = TRUE)) ``` -### Example 5: alluvial chart, REACH themed + +### Example 5: waffle chart +```{r example-waffle-plot, out.width = "65%", warning = FALSE} +# +waffle(df, status, percentage, x_title = "A caption", title = "A title", subtitle = "A subtitle") +``` + + +### Example 6: 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 @@ -200,7 +209,7 @@ alluvial(df, ``` -### Example 6: lollipop chart +### Example 7: lollipop chart ```{r example-lollipop-chart, out.width = "65%", warning = FALSE} library(tidyr) # Prepare long data diff --git a/README.md b/README.md index c2e6914..0e24eb5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # visualizeR -> What a color! What a viz! +> What a color\! What a viz\! `visualizeR` proposes some utils to get REACH and AGORA colors, ready-to-go color palettes, and a few visualization functions @@ -23,16 +23,16 @@ devtools::install_github("gnoblet/visualizeR", build_vignettes = TRUE) 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 + - \[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 -- [x] Add new types of visualization (e.g. dumbbell plot, lollipop + - \[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 + - \[X\] Use examples + - \[ \] Add some ease-map functions + - \[ \] Add some interactive functions (maps and graphs) + - \[ \] Consolidate and make errors transparent ## Request @@ -98,6 +98,7 @@ bar(df, island, mean_bl, species, percent = FALSE, alpha = 0.6, x_title = "Mean ``` 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")) ``` @@ -105,6 +106,7 @@ bar(df, island,mean_bl, species, percent = TRUE, theme = theme_reach(palette = " ``` 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)) ``` @@ -117,6 +119,7 @@ At this stage, `point_reach()` only supports categorical grouping colors with the `group` arg. ``` r + # Simple point chart point(penguins, bill_length_mm, flipper_length_mm) ``` @@ -124,6 +127,7 @@ 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)) ``` @@ -131,6 +135,7 @@ point(penguins, bill_length_mm, flipper_length_mm, island, alpha = 0.6, size = 3 ``` 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_major_x = TRUE, title_position_to_plot = FALSE)) ``` @@ -153,6 +158,7 @@ df <- tibble::tibble( dplyr::mutate(stat = round(stat, 0)) # Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title + dumbbell(df, stat, setting, @@ -175,9 +181,10 @@ dumbbell(df, -### Example 4: donut chart, REACH themed (to used moderately) +### Example 4: donut chart, REACH themed (to used once, not twice) ``` 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"), @@ -199,9 +206,19 @@ donut(df, -### Example 5: alluvial chart, REACH themed +### Example 5: waffle chart ``` r +# +waffle(df, status, percentage, x_title = "A caption", title = "A title", subtitle = "A subtitle") +``` + + + +### Example 6: 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), @@ -230,7 +247,7 @@ alluvial(df, -### Example 6: lollipop chart +### Example 7: lollipop chart ``` r library(tidyr) @@ -260,6 +277,7 @@ lollipop(df, ``` r + # Horizontal, greater point size, arranged by value, no grid, and text labels added lollipop(df, admin1, @@ -281,6 +299,7 @@ lollipop(df, ## Maps ``` r + # Add indicator layer # - based on "pretty" classes and title "Proportion (%)" # - buffer to add a 10% around the bounding box diff --git a/man/figures/README-example-alluvial-plot-1.png b/man/figures/README-example-alluvial-plot-1.png index f1c9284..33a0499 100644 Binary files a/man/figures/README-example-alluvial-plot-1.png and b/man/figures/README-example-alluvial-plot-1.png differ diff --git a/man/figures/README-example-bar-chart-1.png b/man/figures/README-example-bar-chart-1.png index 246c204..cc3c997 100644 Binary files a/man/figures/README-example-bar-chart-1.png and b/man/figures/README-example-bar-chart-1.png differ diff --git a/man/figures/README-example-bar-chart-2.png b/man/figures/README-example-bar-chart-2.png index f7a82d5..e5cbaad 100644 Binary files a/man/figures/README-example-bar-chart-2.png and b/man/figures/README-example-bar-chart-2.png differ diff --git a/man/figures/README-example-bar-chart-3.png b/man/figures/README-example-bar-chart-3.png index 4066025..f4b8b7e 100644 Binary files a/man/figures/README-example-bar-chart-3.png and b/man/figures/README-example-bar-chart-3.png differ diff --git a/man/figures/README-example-donut-plot-1.png b/man/figures/README-example-donut-plot-1.png index 06aaf7a..d30c943 100644 Binary files a/man/figures/README-example-donut-plot-1.png and b/man/figures/README-example-donut-plot-1.png differ diff --git a/man/figures/README-example-dumbbell-plot-1.png b/man/figures/README-example-dumbbell-plot-1.png index 9236eb9..06ead24 100644 Binary files a/man/figures/README-example-dumbbell-plot-1.png and b/man/figures/README-example-dumbbell-plot-1.png differ diff --git a/man/figures/README-example-lollipop-chart-1.png b/man/figures/README-example-lollipop-chart-1.png index 2bc316c..0decaf1 100644 Binary files a/man/figures/README-example-lollipop-chart-1.png and b/man/figures/README-example-lollipop-chart-1.png differ diff --git a/man/figures/README-example-lollipop-chart-2.png b/man/figures/README-example-lollipop-chart-2.png index d6c4582..0897e8e 100644 Binary files a/man/figures/README-example-lollipop-chart-2.png and b/man/figures/README-example-lollipop-chart-2.png differ diff --git a/man/figures/README-example-map.png b/man/figures/README-example-map.png index eb3162e..3e671f9 100644 Binary files a/man/figures/README-example-map.png and b/man/figures/README-example-map.png differ diff --git a/man/figures/README-example-point-chart-1.png b/man/figures/README-example-point-chart-1.png index de23c88..f705a3d 100644 Binary files a/man/figures/README-example-point-chart-1.png and b/man/figures/README-example-point-chart-1.png differ diff --git a/man/figures/README-example-point-chart-2.png b/man/figures/README-example-point-chart-2.png index 9bfbd50..76ef877 100644 Binary files a/man/figures/README-example-point-chart-2.png and b/man/figures/README-example-point-chart-2.png differ diff --git a/man/figures/README-example-point-chart-3.png b/man/figures/README-example-point-chart-3.png index d802343..b32aa89 100644 Binary files a/man/figures/README-example-point-chart-3.png and b/man/figures/README-example-point-chart-3.png differ diff --git a/man/figures/README-example-waffke-plot-1.png b/man/figures/README-example-waffke-plot-1.png new file mode 100644 index 0000000..a750983 Binary files /dev/null and b/man/figures/README-example-waffke-plot-1.png differ diff --git a/man/figures/README-example-waffle-plot-1.png b/man/figures/README-example-waffle-plot-1.png new file mode 100644 index 0000000..0183c5a Binary files /dev/null and b/man/figures/README-example-waffle-plot-1.png differ diff --git a/man/pal_fallback.Rd b/man/pal_fallback.Rd new file mode 100644 index 0000000..6716ae5 --- /dev/null +++ b/man/pal_fallback.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pal_fallback.R +\name{pal_fallback} +\alias{pal_fallback} +\title{Return function to interpolate a fallback palette base on viridis::magma()} +\usage{ +pal_fallback( + reverse = FALSE, + color_ramp_palette = FALSE, + discrete = FALSE, + n = 5, + ... +) +} +\arguments{ +\item{reverse}{Boolean indicating whether the palette should be reversed} + +\item{color_ramp_palette}{Should the output be a `grDevices::colorRampPalette` function or a vector of hex codes? Default to the latter with `FALSE`} + +\item{discrete}{Boolean. Discrete or not? Default to FALSE.} + +\item{n}{Number of colors in the palette. Default to 5. Passe to `viridis::magma()`} + +\item{...}{Other parameters to pass to `grDevices::colorRampPalette()`} +} +\value{ +A color palette +} +\description{ +Return function to interpolate a fallback palette base on viridis::magma() +} diff --git a/man/scale_color.Rd b/man/scale_color.Rd index c05dcf9..ab17204 100644 --- a/man/scale_color.Rd +++ b/man/scale_color.Rd @@ -14,7 +14,7 @@ scale_color( ) } \arguments{ -\item{initiative}{Either "reach" or "agora.} +\item{initiative}{Either "reach" or "agora" or "default".} \item{palette}{Palette name from `pal_reach()` or `pal_agora()`.} diff --git a/man/scale_fill.Rd b/man/scale_fill.Rd index b07d1a8..95d3dc3 100644 --- a/man/scale_fill.Rd +++ b/man/scale_fill.Rd @@ -14,7 +14,7 @@ scale_fill( ) } \arguments{ -\item{initiative}{Either "reach" or "agora.} +\item{initiative}{Either "reach" or "agora" or "default".} \item{palette}{Palette name from `pal_reach()` or `pal_agora()`.} diff --git a/man/theme_reach.Rd b/man/theme_reach.Rd index 6f0c50d..8718dc6 100644 --- a/man/theme_reach.Rd +++ b/man/theme_reach.Rd @@ -5,6 +5,7 @@ \title{ggplot2 theme with REACH color palettes} \usage{ theme_reach( + initiative = "reach", palette = "main", discrete = TRUE, reverse = FALSE, @@ -12,11 +13,14 @@ theme_reach( title_size = 12, title_color = cols_reach("main_grey"), title_font_face = "bold", + title_hjust = NULL, title_position_to_plot = TRUE, text_size = 10, 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, @@ -52,13 +56,15 @@ theme_reach( ) } \arguments{ +\item{initiative}{Either "reach" or "default".} + \item{palette}{Palette name from 'pal_reach()'.} \item{discrete}{Boolean indicating whether color aesthetic is discrete or not.} \item{reverse}{Boolean indicating whether the palette should be reversed.} -\item{font_family}{The font family for all plot's texts. Default to "Leelawadee".} +\item{font_family}{The font family for all plot's texts. Default to "Segoe UI".} \item{title_size}{The size of the legend title. Defaults to 11.} @@ -66,6 +72,8 @@ theme_reach( \item{title_font_face}{Legend title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").} +\item{title_hjust}{Title horizontal justification. Default to NULL. Use 0.5 to center the title.} + \item{title_position_to_plot}{TRUE or FALSE. Positioning to plot or to panel?} \item{text_size}{The size of all text other than the title, subtitle and caption. Defaults to 10.} @@ -76,6 +84,10 @@ theme_reach( \item{panel_background_color}{The color for the panel background color. Default to white.} +\item{panel_border}{Boolean. Plot a panel border? Default to FALSE.} + +\item{panel_border_color}{A color. Default to REACH main grey.} + \item{legend_position}{Position of the legend; Default to "right". Can take "right", "left", "top", "bottom" or "none".} \item{legend_direction}{Direction of the legend. Default to "vertical". Can take "vertical" or "horizontal".} diff --git a/man/visualizeR-package.Rd b/man/visualizeR-package.Rd new file mode 100644 index 0000000..25e9400 --- /dev/null +++ b/man/visualizeR-package.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/visualizeR-package.R +\docType{package} +\name{visualizeR-package} +\alias{visualizeR} +\alias{visualizeR-package} +\title{visualizeR: What a color! What a viz!} +\description{ +\if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}} + +It basically provides colors as hex codes, color palettes, and some viz functions (graphs and maps). +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/gnoblet/visualizeR} + \item \url{https://gnoblet.github.io/visualizeR/} +} + +} +\author{ +\strong{Maintainer}: Noblet Guillaume \email{gnoblet@zaclys.net} + +} +\keyword{internal} diff --git a/man/waffle.Rd b/man/waffle.Rd new file mode 100644 index 0000000..a46d9aa --- /dev/null +++ b/man/waffle.Rd @@ -0,0 +1,53 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/waffle.R +\name{waffle} +\alias{waffle} +\title{Simple waffle chart} +\usage{ +waffle( + df, + x, + y, + n_rows = 10, + size = 2, + x_title = NULL, + x_lab = NULL, + title = NULL, + subtitle = NULL, + caption = NULL, + arrange = TRUE, + theme = theme_reach(axis_x = FALSE, axis_y = FALSE, legend_position = "bottom", + legend_direction = "horizontal", title_hjust = 0.5) +) +} +\arguments{ +\item{df}{A data frame.} + +\item{x}{A character column or coercible as a character column. Will give the waffle's fill color.} + +\item{y}{A numeric column (if plotting proportion, make sure to have percentages between 0 and 100 and not 0 and 1).} + +\item{n_rows}{Number of rows. Default to 10.} + +\item{size}{Width of the separator between blocks (defaults to 2).} + +\item{x_title}{The x scale title. Default to NULL.} + +\item{x_lab}{The x scale caption. Default to NULL.} + +\item{title}{Plot title. Default to NULL.} + +\item{subtitle}{Plot subtitle. Default to NULL.} + +\item{caption}{Plot caption. Default to NULL.} + +\item{arrange}{TRUE or FALSE. Arrange by highest percentage first.} + +\item{theme}{Whatever theme. Default to theme_reach().} +} +\value{ +A waffle chart +} +\description{ +Simple waffle chart +}