Merge pull request #18 from gnoblet/gen-dev

Gen dev
This commit is contained in:
Guillaume NOBLET 2023-05-29 11:19:43 +02:00 committed by GitHub
commit 12ff1d2cce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 510 additions and 101 deletions

View file

@ -1,7 +1,7 @@
Package: visualizeR Package: visualizeR
Type: Package Type: Package
Title: What a color! What a viz! Title: What a color! What a viz!
Version: 0.7.9000 Version: 0.8.9000
Authors@R: c( Authors@R: c(
person( person(
'Noblet', 'Guillaume', 'Noblet', 'Guillaume',
@ -20,7 +20,7 @@ LazyData: true
RoxygenNote: 7.2.3 RoxygenNote: 7.2.3
Imports: Imports:
ggplot2, ggplot2,
rlang, rlang (>= 0.4.11),
grDevices, grDevices,
glue, glue,
scales, scales,
@ -28,6 +28,12 @@ Imports:
ggrepel, ggrepel,
tidyr, tidyr,
dplyr, dplyr,
ggalluvial ggalluvial,
Suggests: knitr, sf, tmap viridisLite,
waffle
Suggests:
knitr,
roxygen2,
sf,
tmap
VignetteBuilder: knitr VignetteBuilder: knitr

11
NEWS.md
View file

@ -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 `dumbbell()`.
* Add `alluvial()` * Add `alluvial()`

View file

@ -87,23 +87,21 @@ donut <- function(df,
ggplot2::coord_polar( ggplot2::coord_polar(
theta = "y" 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 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
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 # Add theme
g <- g + 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) return(g)
} }

View file

@ -13,15 +13,9 @@ pal_agora <- function(palette = "main", reverse = FALSE, color_ramp_palette = FA
palettes_agora <- list( palettes_agora <- list(
`main` = cols_agora("main_grey", "main_red", "main_lt_grey", "main_beige"), `main` = cols_agora("main_bordeaux", "main_dk_beige", "main_lt_grey", "main_lt_beige"),
`primary` = cols_agora("main_grey", "main_red"), `primary` = cols_agora("main_bordeaux", "main_dk_beige"),
`secondary` = cols_agora("main_lt_grey", "main_beige"), `secondary` = cols_agora( "main_lt_grey", "main_lt_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")
) )
if (show_palettes) return(names(palettes_agora)) if (show_palettes) return(names(palettes_agora))

30
R/pal_fallback.R Normal file
View file

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

154
R/scale.R
View file

@ -1,6 +1,6 @@
#' Color scale constructor for REACH or AGORA colors #' 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 palette Palette name from `pal_reach()` or `pal_agora()`.
#' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param discrete Boolean indicating whether color aesthetic is discrete or not.
#' @param reverse Boolean indicating whether the palette should be reversed. #' @param reverse Boolean indicating whether the palette should be reversed.
@ -14,21 +14,82 @@
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") { if (initiative == "reach") {
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( pal <- pal_reach(
palette = palette, palette = palette,
reverse = reverse, reverse = reverse,
color_ramp_palette = TRUE, color_ramp_palette = TRUE,
show_palettes = FALSE show_palettes = FALSE
) )
}
} else if (initiative == "agora") { } else if (initiative == "agora") {
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( pal <- pal_agora(
palette = palette, palette = palette,
reverse = reverse, reverse = reverse,
color_ramp_palette = TRUE, color_ramp_palette = TRUE,
show_palettes = FALSE show_palettes = FALSE
) )
}
} else if (initiative == "default") {
pal <- pal_fallback(
reverse = reverse,
discrete = discrete,
color_ramp_palette = TRUE)
if (discrete) palette <- "viridis" else palette <- "magma"
} else { } 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) { if (discrete) {
@ -41,8 +102,10 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE
draw.ulim = TRUE, draw.ulim = TRUE,
draw.llim = TRUE, draw.llim = TRUE,
ticks.colour = "#F1F3F5", ticks.colour = "#F1F3F5",
reverse = reverse_guide), reverse = reverse_guide
...) ),
...
)
} else { } else {
ggplot2::scale_color_gradientn( ggplot2::scale_color_gradientn(
colours = pal(256), colours = pal(256),
@ -53,7 +116,8 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE
ticks.colour = "#F1F3F5", ticks.colour = "#F1F3F5",
reverse = reverse_guide reverse = reverse_guide
), ),
...) ...
)
} }
} }
@ -61,7 +125,7 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE
#' Fill scale constructor for REACH or AGORA colors #' 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 palette Palette name from `pal_reach()` or `pal_agora()`.
#' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param discrete Boolean indicating whether color aesthetic is discrete or not.
#' @param reverse Boolean indicating whether the palette should be reversed. #' @param reverse Boolean indicating whether the palette should be reversed.
@ -74,22 +138,84 @@ scale_color <- function(initiative = "reach", palette = "main", discrete = TRUE
#' @export #' @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") { if (initiative == "reach") {
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( pal <- pal_reach(
palette = palette, palette = palette,
reverse = reverse, reverse = reverse,
color_ramp_palette = TRUE, color_ramp_palette = TRUE,
show_palettes = FALSE show_palettes = FALSE
) )
}
} else if (initiative == "agora") { } else if (initiative == "agora") {
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( pal <- pal_agora(
palette = palette, palette = palette,
reverse = reverse, reverse = reverse,
color_ramp_palette = TRUE, color_ramp_palette = TRUE,
show_palettes = FALSE show_palettes = FALSE
) )
}
} else if (initiative == "default") {
pal <- pal_fallback(
reverse = reverse,
discrete = discrete,
color_ramp_palette = TRUE)
if (discrete) palette <- "viridis" else palette <- "magma"
} else { } 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) { if (discrete) {
@ -102,17 +228,21 @@ scale_fill <- function(initiative = "reach", palette = "main", discrete = TRUE,
draw.ulim = TRUE, draw.ulim = TRUE,
draw.llim = TRUE, draw.llim = TRUE,
ticks.colour = "#F1F3F5", ticks.colour = "#F1F3F5",
reverse = reverse_guide), reverse = reverse_guide
...) ),
...
)
} else { } else {
ggplot2::scale_fill_gradientn( ggplot2::scale_color_gradientn(
colours = pal(256), colours = pal(256),
guide = ggplot2::guide_colorbar( guide = ggplot2::guide_colorbar(
title.position = "top", title.position = "top",
draw.ulim = TRUE, draw.ulim = TRUE,
draw.llim = TRUE, draw.llim = TRUE,
ticks.colour = "#F1F3F5", ticks.colour = "#F1F3F5",
reverse = reverse_guide), reverse = reverse_guide
...) ),
...
)
} }
} }

View file

@ -1,16 +1,20 @@
#' @title ggplot2 theme with REACH color palettes #' @title ggplot2 theme with REACH color palettes
#' #'
#' @param initiative Either "reach" or "default".
#' @param palette Palette name from 'pal_reach()'. #' @param palette Palette name from 'pal_reach()'.
#' @param discrete Boolean indicating whether color aesthetic is discrete or not. #' @param discrete Boolean indicating whether color aesthetic is discrete or not.
#' @param reverse Boolean indicating whether the palette should be reversed. #' @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_size The size of the title. Defaults to 12.
#' @param title_color Title color. #' @param title_color Title color.
#' @param title_font_face Title font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic"). #' @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_size The size of all text other than the title, subtitle and caption. Defaults to 10.
#' @param text_color Text color. #' @param text_color Text color.
#' @param text_font_face Text font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic"). #' @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_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_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_direction Direction of the legend. Default to "vertical". Can take "vertical" or "horizontal".
#' @param legend_title_size Legend title size. #' @param legend_title_size Legend title size.
@ -55,6 +59,7 @@
#' #'
#' @export #' @export
theme_reach <- function( theme_reach <- function(
initiative = "reach",
palette = "main", palette = "main",
discrete = TRUE, discrete = TRUE,
reverse = FALSE, reverse = FALSE,
@ -62,11 +67,14 @@ theme_reach <- function(
title_size = 12, title_size = 12,
title_color = cols_reach("main_grey"), title_color = cols_reach("main_grey"),
title_font_face = "bold", title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE, title_position_to_plot = TRUE,
text_size = 10, text_size = 10,
text_color = cols_reach("main_grey"), text_color = cols_reach("main_grey"),
text_font_face = "plain", text_font_face = "plain",
panel_background_color = "#FFFFFF", panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = cols_reach("main_grey"),
legend_position = "right", legend_position = "right",
legend_direction = "vertical", legend_direction = "vertical",
legend_reverse = TRUE, legend_reverse = TRUE,
@ -104,6 +112,14 @@ theme_reach <- function(
# To do : # To do :
# - add facet theming # - 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 # Basic simple theme
# theme_reach <- ggplot2::theme_bw() # theme_reach <- ggplot2::theme_bw()
@ -145,9 +161,13 @@ theme_reach <- function(
face = axis_title_font_face, face = axis_title_font_face,
color = axis_title_color), color = axis_title_color),
# Wrap title # Wrap title
plot.title = ggtext::element_textbox_simple(), plot.title = ggtext::element_textbox(
plot.subtitle = ggtext::element_textbox_simple(), hjust = title_hjust
plot.caption = ggtext::element_textbox_simple(), ),
plot.subtitle = ggtext::element_textbox(
hjust = title_hjust
),
plot.caption = ggtext::element_textbox(),
legend.title = ggplot2::element_text( legend.title = ggplot2::element_text(
size = legend_title_size, size = legend_title_size,
face = legend_title_font_face, face = legend_title_font_face,
@ -244,32 +264,24 @@ theme_reach <- function(
color = grid_minor_color, color = grid_minor_color,
linewidth = grid_minor_y_size) 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 # Other parameters
theme_reach <- theme_reach + ggplot2::theme(...) 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 # Add reach color palettes by default
# (reversed guide is defaulted to TRUE for natural reading) # (reversed guide is defaulted to TRUE for natural reading)
theme_reach <- list( theme_reach <- list(
theme_reach, theme_reach,
scale_color(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(palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse) scale_fill(initiative = initiative, palette = palette, discrete = discrete, reverse = reverse, reverse_guide = legend_reverse)
) )

7
R/visualizeR-package.R Normal file
View file

@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"
## usethis namespace: start
#' @importFrom rlang :=
## usethis namespace: end
NULL

74
R/waffle.R Normal file
View file

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

View file

@ -128,6 +128,7 @@ df <- tibble::tibble(
dplyr::mutate(stat = round(stat, 0)) dplyr::mutate(stat = round(stat, 0))
# Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title # Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title
dumbbell(df, dumbbell(df,
stat, stat,
setting, 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} ```{r example-donut-plot, out.width = "65%", warning = FALSE}
# Some summarized data: % of HHs by displacement status # Some summarized data: % of HHs by displacement status
@ -170,7 +171,15 @@ donut(df,
theme = theme_reach(legend_reverse = TRUE)) 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} ```{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 # 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} ```{r example-lollipop-chart, out.width = "65%", warning = FALSE}
library(tidyr) library(tidyr)
# Prepare long data # Prepare long data

View file

@ -3,7 +3,7 @@
# visualizeR <img src="man/figures/logo.png" align="right" alt="" width="120"/> # visualizeR <img src="man/figures/logo.png" align="right" alt="" width="120"/>
> What a color! What a viz! > What a color\! What a viz\!
`visualizeR` proposes some utils to get REACH and AGORA colors, `visualizeR` proposes some utils to get REACH and AGORA colors,
ready-to-go color palettes, and a few visualization functions 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: Roadmap is as follows:
- [x] Add IMPACTs colors - \[X\] Add IMPACTs colors
- [x] Add all color palettes from the internal documentation - \[X\] Add all color palettes from the internal documentation
- [ ] There remains to be added more-than-7-color palettes and black - \[ \] There remains to be added more-than-7-color palettes and black
color palettes 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.) plot, etc.)
- [x] Use examples - \[X\] Use examples
- [ ] Add some ease-map functions - \[ \] Add some ease-map functions
- [ ] Add some interactive functions (maps and graphs) - \[ \] Add some interactive functions (maps and graphs)
- [ ] Consolidate and make errors transparent - \[ \] Consolidate and make errors transparent
## Request ## Request
@ -98,6 +98,7 @@ bar(df, island, mean_bl, species, percent = FALSE, alpha = 0.6, x_title = "Mean
<img src="man/figures/README-example-bar-chart-1.png" width="65%" /> <img src="man/figures/README-example-bar-chart-1.png" width="65%" />
``` r ``` r
# Using another color palette through `theme_reach()` and changing scale to percent # 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")) 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 = "
<img src="man/figures/README-example-bar-chart-2.png" width="65%" /> <img src="man/figures/README-example-bar-chart-2.png" width="65%" />
``` r ``` r
# Not flipped, with text added, group_title, no y-axis and no bold for legend # 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)) 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. with the `group` arg.
``` r ``` r
# Simple point chart # Simple point chart
point(penguins, bill_length_mm, flipper_length_mm) point(penguins, bill_length_mm, flipper_length_mm)
``` ```
@ -124,6 +127,7 @@ point(penguins, bill_length_mm, flipper_length_mm)
<img src="man/figures/README-example-point-chart-1.png" width="65%" /> <img src="man/figures/README-example-point-chart-1.png" width="65%" />
``` r ``` r
# Point chart with grouping colors, greater dot size, some transparency, reversed color palette # 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)) 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
<img src="man/figures/README-example-point-chart-2.png" width="65%" /> <img src="man/figures/README-example-point-chart-2.png" width="65%" />
``` r ``` r
# Using another color palettes # 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)) 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)) dplyr::mutate(stat = round(stat, 0))
# Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title # Example, adding a parameter to `theme_reach()` passed on `ggplot2::theme()` to align legend title
dumbbell(df, dumbbell(df,
stat, stat,
setting, setting,
@ -175,9 +181,10 @@ dumbbell(df,
<img src="man/figures/README-example-dumbbell-plot-1.png" width="65%" /> <img src="man/figures/README-example-dumbbell-plot-1.png" width="65%" />
### Example 4: donut chart, REACH themed (to used moderately) ### Example 4: donut chart, REACH themed (to used once, not twice)
``` r ``` r
# Some summarized data: % of HHs by displacement status # Some summarized data: % of HHs by displacement status
df <- tibble::tibble( df <- tibble::tibble(
status = c("Displaced", "Non displaced", "Returnee", "Don't know/Prefer not to say"), status = c("Displaced", "Non displaced", "Returnee", "Don't know/Prefer not to say"),
@ -199,9 +206,19 @@ donut(df,
<img src="man/figures/README-example-donut-plot-1.png" width="65%" /> <img src="man/figures/README-example-donut-plot-1.png" width="65%" />
### Example 5: alluvial chart, REACH themed ### Example 5: waffle chart
``` r ``` r
#
waffle(df, status, percentage, x_title = "A caption", title = "A title", subtitle = "A subtitle")
```
<img src="man/figures/README-example-waffle-plot-1.png" width="65%" />
### Example 6: alluvial chart, REACH themed
``` r
# Some summarized data: % of HHs by self-reported status of displacement in 2021 and in 2022 # Some summarized data: % of HHs by self-reported status of displacement in 2021 and in 2022
df <- tibble::tibble( df <- tibble::tibble(
status_from = c(rep("Displaced", 4), status_from = c(rep("Displaced", 4),
@ -230,7 +247,7 @@ alluvial(df,
<img src="man/figures/README-example-alluvial-plot-1.png" width="65%" /> <img src="man/figures/README-example-alluvial-plot-1.png" width="65%" />
### Example 6: lollipop chart ### Example 7: lollipop chart
``` r ``` r
library(tidyr) library(tidyr)
@ -260,6 +277,7 @@ lollipop(df,
<img src="man/figures/README-example-lollipop-chart-1.png" width="65%" /> <img src="man/figures/README-example-lollipop-chart-1.png" width="65%" />
``` r ``` r
# Horizontal, greater point size, arranged by value, no grid, and text labels added # Horizontal, greater point size, arranged by value, no grid, and text labels added
lollipop(df, lollipop(df,
admin1, admin1,
@ -281,6 +299,7 @@ lollipop(df,
## Maps ## Maps
``` r ``` r
# Add indicator layer # Add indicator layer
# - based on "pretty" classes and title "Proportion (%)" # - based on "pretty" classes and title "Proportion (%)"
# - buffer to add a 10% around the bounding box # - buffer to add a 10% around the bounding box

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 KiB

After

Width:  |  Height:  |  Size: 239 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

After

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 182 KiB

After

Width:  |  Height:  |  Size: 177 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 285 KiB

After

Width:  |  Height:  |  Size: 281 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 319 KiB

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 166 KiB

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

31
man/pal_fallback.Rd Normal file
View file

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

View file

@ -14,7 +14,7 @@ scale_color(
) )
} }
\arguments{ \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()`.} \item{palette}{Palette name from `pal_reach()` or `pal_agora()`.}

View file

@ -14,7 +14,7 @@ scale_fill(
) )
} }
\arguments{ \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()`.} \item{palette}{Palette name from `pal_reach()` or `pal_agora()`.}

View file

@ -5,6 +5,7 @@
\title{ggplot2 theme with REACH color palettes} \title{ggplot2 theme with REACH color palettes}
\usage{ \usage{
theme_reach( theme_reach(
initiative = "reach",
palette = "main", palette = "main",
discrete = TRUE, discrete = TRUE,
reverse = FALSE, reverse = FALSE,
@ -12,11 +13,14 @@ theme_reach(
title_size = 12, title_size = 12,
title_color = cols_reach("main_grey"), title_color = cols_reach("main_grey"),
title_font_face = "bold", title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE, title_position_to_plot = TRUE,
text_size = 10, text_size = 10,
text_color = cols_reach("main_grey"), text_color = cols_reach("main_grey"),
text_font_face = "plain", text_font_face = "plain",
panel_background_color = "#FFFFFF", panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = cols_reach("main_grey"),
legend_position = "right", legend_position = "right",
legend_direction = "vertical", legend_direction = "vertical",
legend_reverse = TRUE, legend_reverse = TRUE,
@ -52,13 +56,15 @@ theme_reach(
) )
} }
\arguments{ \arguments{
\item{initiative}{Either "reach" or "default".}
\item{palette}{Palette name from 'pal_reach()'.} \item{palette}{Palette name from 'pal_reach()'.}
\item{discrete}{Boolean indicating whether color aesthetic is discrete or not.} \item{discrete}{Boolean indicating whether color aesthetic is discrete or not.}
\item{reverse}{Boolean indicating whether the palette should be reversed.} \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.} \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_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{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.} \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_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_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".} \item{legend_direction}{Direction of the legend. Default to "vertical". Can take "vertical" or "horizontal".}

25
man/visualizeR-package.Rd Normal file
View file

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

53
man/waffle.Rd Normal file
View file

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