diff --git a/DESCRIPTION b/DESCRIPTION index 20aabba..80c3d5d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: visualizeR Type: Package Title: What a color! What a viz! -Version: 0.4.9000 +Version: 0.5.9000 Authors@R: c( person( 'Noblet', 'Guillaume', @@ -23,6 +23,7 @@ Imports: rlang, grDevices, glue, - scales + scales, + ggtext Suggests: knitr, sf, tmap VignetteBuilder: knitr diff --git a/NEWS.md b/NEWS.md index eb52999..1c505d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# visualizeR 0.5.9000 + +* Add wrapping of title, subtitle and caption thanks to `ggtext` +* Add wrapping of labels for `bar()` x-discrete scale. +* Add further parameters to `theme_reach()` + +--- + # visualizeR 0.4.9000 * Breaking changes: remove dependency to `ggblanket`. diff --git a/R/bar.R b/R/bar.R index 24ddae8..895eb92 100644 --- a/R/bar.R +++ b/R/bar.R @@ -6,6 +6,7 @@ #' @param group Some grouping categorical column, e.g. administrative areas or population groups. #' @param flip TRUE or FALSE. Default to TRUE or horizontal bar plot. #' @param percent TRUE or FALSE. Should the x-labels (and text labels if present) be displayed as percentages? Default to TRUE. +#' @param wrap Should x-labels be wrapped? Number of characters. #' @param position Should the chart be stacked? Default to "dodge". Can take "dodge" and "stack". #' @param alpha Fill transparency. #' @param x_title The x scale title. Default to NULL. @@ -21,7 +22,7 @@ #' @return A bar chart #' #' @export -bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, position = "dodge", alpha = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, add_text = FALSE, add_text_suffix = "", theme = theme_reach()){ +bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, wrap = NULL, position = "dodge", alpha = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, add_text = FALSE, add_text_suffix = "", theme = theme_reach()){ # To do : # - automate bar width and text size, or at least give the flexibility and still center text @@ -88,6 +89,10 @@ bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, position = g <- g + ggplot2::scale_y_continuous(expand = c(0.01, 0.1)) } + 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() diff --git a/R/point.R b/R/point.R index 3ef72c1..9dae4bf 100644 --- a/R/point.R +++ b/R/point.R @@ -1,4 +1,4 @@ -#' @title Simple bar chart +#' @title Simple point chart #' #' @param df A data frame. #' @param x A numeric column. diff --git a/R/theme_reach.R b/R/theme_reach.R index 254f5e9..15f95cf 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -13,14 +13,28 @@ #' @param panel_background_color The color for the panel background color. Default to white. #' @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. +#' @param legend_title_color Legend title color. +#' @param legend_title_font_face Legend title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic"). #' @param legend_reverse Reverse the color in the guide? Default to TRUE. +#' @param title_size The size of the legend title. Defaults to 11. +#' @param title_color Legend title color. +#' @param title_font_face Legend title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic"). +#' @param title_position_to_plot TRUE or FALSE. Positioning to plot or to panel? #' @param axis_x Boolean. Do you need x-axis? #' @param axis_y Boolean. Do you need y-axis? +#' @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_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 caption_position_to_plot TRUE or FALSE. Positioning to plot or to panel? #' @param ... Additional arguments passed to `ggplot2::gg_theme()`. #' #' @@ -37,20 +51,31 @@ theme_reach <- function( title_size = 12, title_color = cols_reach("main_grey"), title_font_face = "bold", + title_position_to_plot = TRUE, text_size = 10, text_color = cols_reach("main_grey"), - text_font_face = "bold", + text_font_face = "plain", panel_background_color = "#FFFFFF", legend_position = "right", legend_direction = "vertical", legend_reverse = TRUE, + legend_title_size = 11, + legend_title_color = cols_reach("main_grey"), + legend_title_font_face = "plain", axis_x = TRUE, axis_y = TRUE, + axis_text_size = 10, + axis_text_color = cols_reach("main_grey"), + axis_text_font_face = "plain", + 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, + caption_position_to_plot = TRUE, ... ) { @@ -87,9 +112,42 @@ theme_reach <- function( panel.grid.minor.x = ggplot2::element_blank(), panel.grid.minor.y = ggplot2::element_blank(), # Remove background for legend key - legend.key = ggplot2::element_blank() + legend.key = ggplot2::element_blank(), + # Text sizes + axis.text = ggplot2::element_text( + size = axis_text_size, + family = font_family, + face = axis_text_font_face, + color = axis_text_color + ), + axis.title = ggplot2::element_text( + size = axis_title_size, + family = font_family, + 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(), + legend.title = ggplot2::element_text( + size = legend_title_size, + face = legend_title_font_face, + family = font_family, + color = legend_title_color) ) + # Position of title + if (title_position_to_plot) theme_reach <- theme_reach + + ggplot2::theme( + plot.title.position = "plot" + ) + + if (caption_position_to_plot) theme_reach <- theme_reach + + ggplot2::theme( + plot.caption.position = "plot" + ) + # Position of caption + # Axis lines ? if (axis_x & axis_y) { theme_reach <- theme_reach + diff --git a/README.Rmd b/README.Rmd index 16cdf8e..72ea8c9 100644 --- a/README.Rmd +++ b/README.Rmd @@ -109,7 +109,7 @@ 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)) +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)) ``` ## Maps diff --git a/README.md b/README.md index 79104a1..01231ad 100644 --- a/README.md +++ b/README.md @@ -135,7 +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_x = T)) +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)) ``` diff --git a/docs/404.html b/docs/404.html index b309118..5eb2dff 100644 --- a/docs/404.html +++ b/docs/404.html @@ -31,7 +31,7 @@ visualizeR - 0.4.9000 + 0.5.9000