diff --git a/R/theme_reach.R b/R/theme_reach.R index ba20f6d..7084301 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -7,6 +7,7 @@ #' @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"). @@ -62,6 +63,7 @@ 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"), @@ -147,9 +149,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, diff --git a/R/waffle.R b/R/waffle.R index ae6109a..781f663 100644 --- a/R/waffle.R +++ b/R/waffle.R @@ -93,3 +93,82 @@ waffle <- function(df, return(g) } + +# +# # Make a waffle plot with ggplot2 that shows 100 squares and the already summarized proportions of the following data frame as input: df <- data.frame(category = c("Category 1", "Category 2", "Category 3"),proportion = c(0.1, 0.8, 0.1)) +# +# +# # Make a waffle plot using ggplot2 that shows 100 squares and the already summarized proportions of the following data frame as input: df <- data.frame(category = c("Category 1", "Category 2", "Category 3"),proportion = c(0.1, 0.8, 0.1)) +# +# # Load the ggplot2 package +# library(ggplot2) +# +# # Create a vector with the number of squares for each category +# num_squares <- round(df$proportion * 100) +# +# # Create a vector with the category labels (repeated by the number of squares) +# labels <- rep(df$category, times = num_squares) +# +# # Create a data frame with the labels and a numeric variable for the x and y positions +# waffle_df <- data.frame( +# x = rep(1:10, times = 10), +# y = rep(1:10, each = 10), +# label = labels +# ) +# +# # Create the plot using ggplot2 +# ggplot(waffle_df, aes(x = x, y = y, fill = label)) + +# geom_tile(color = "white", size = 0.5) + +# scale_fill_manual(values = c("#F8766D", "#00BA38", "#619CFF")) + +# theme_void() +# # Load the ggplot2 package +# library(ggplot2) +# +# # Create a vector of labels for the categories +# labels <- df$category +# +# # Create a vector with the number of squares for each category, based on the proportions +# numsquares <- round(df$proportion * 100) +# +# # Create a data frame with the labels and the number of squares +# df_waffle <- data.frame( +# category = rep(labels, numsquares), +# square = rep(1:100) +# ) +# +# # Create the plot using ggplot +# ggplot(df_waffle, aes(x = factor(1), fill = category)) + +# geom_bar(stat = "count") + +# scale_fill_brewer(palette = "Set2") + +# coord_polar(theta = "y") + +# theme_void() + +# guides(fill = guide_legend(title = "Category")) +# +# +# +# +# +# +# # Load the 'waffle' package for creating waffle charts +# library(waffle) +# +# # Define the input data +# df <- data.frame( +# category = c("Category 1", "Category 2", "Category 3"), +# proportion = c(10.2, 80, 9.8) +# ) +# +# reach_co +# +# # Create a waffle chart with 100 squares +# waffle::waffle(setNames(df$proportion*100, df$category), rows = 10, +# legend_pos = "bottom", title = "Waffle Chart") + +# theme_reach(axis_x = FALSE, axis_y = FALSE, title_position_to_plot = FALSE, grid_major_x = FALSE, grid_major_y = FALSE, legend_position = "bottom", legend_direction = "horizontal") +# +# ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) +# +# # Add a legend that shows the labels for each category +# legend(x = "bottom", legend = df$category, fill = c("#F8766D", "#00BA38", "#619CFF")) +# +# # waffle plot that shows 100 squares and the already summarized proportions of the following dataframe as input: +# df <- data.frame(category = c("Category 1", "Category 2", "Category 3", "Category 4"), proportion = c(0.1, 0.5, 0.1, 0.3,))