Add draft waffle function
This commit is contained in:
parent
e9e4887f8e
commit
f4b03edcbd
1 changed files with 29 additions and 125 deletions
154
R/waffle.R
154
R/waffle.R
|
|
@ -2,34 +2,44 @@
|
|||
#'
|
||||
#' @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.
|
||||
#' @param alpha Fill transparency.
|
||||
#' @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 to be used parsimoniously
|
||||
#' @return A waffle chart
|
||||
#'
|
||||
#' @export
|
||||
#'
|
||||
#' @example
|
||||
#' df <- data.frame(category = c("Category 1", "Category 2", "Category 3"),proportion = c(15, 55, 30))
|
||||
#' waffle(df, category, proportion, x_title = "A caption", title = "A title", subtitle = "A subtitle")
|
||||
waffle <- function(df,
|
||||
x,
|
||||
y,
|
||||
alpha = 1,
|
||||
n_rows = 10,
|
||||
size = 2,
|
||||
x_title = NULL,
|
||||
x_lab = NULL,
|
||||
title = NULL,
|
||||
subtitle = NULL,
|
||||
caption = NULL,
|
||||
arrange = TRUE,
|
||||
n_rows = 10,
|
||||
size = 0.33,
|
||||
colour = "white",
|
||||
flip = FALSE,
|
||||
theme = theme_reach(
|
||||
|
||||
axis_x = FALSE,
|
||||
axis_y = FALSE)){
|
||||
axis_y = FALSE,
|
||||
legend_position = "bottom",
|
||||
legend_direction = "horizontal",
|
||||
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(
|
||||
|
|
@ -37,29 +47,14 @@ waffle <- function(df,
|
|||
dplyr::desc({{ y }})
|
||||
)
|
||||
|
||||
# Get levels for scaling
|
||||
lev <- dplyr::pull(df, {{ x }})
|
||||
df <- dplyr::mutate(df, "{{x}}" := factor({{ x }}, levels = lev))
|
||||
# Mutate to 100
|
||||
# df <- dplyr::mutate(df, "{{y}}" := {{ y }} * 100)
|
||||
|
||||
# Mapping
|
||||
g <- ggplot2::ggplot(
|
||||
df,
|
||||
mapping = ggplot2::aes(
|
||||
fill = {{ x }},
|
||||
color = {{ x }},
|
||||
values = {{ y }}
|
||||
)
|
||||
)
|
||||
# Prepare named vector
|
||||
values <- stats::setNames(dplyr::pull(df, {{ y }}), dplyr::pull(df, {{ x }}))
|
||||
|
||||
# Add waffle geom
|
||||
g <- g +
|
||||
waffle::geom_waffle(
|
||||
n_rows = n_rows,
|
||||
size = size,
|
||||
color = "white",
|
||||
flip = flip,
|
||||
alpha = alpha) +
|
||||
ggplot2::coord_equal()
|
||||
# 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(
|
||||
|
|
@ -70,22 +65,10 @@ waffle <- function(df,
|
|||
color = x_title
|
||||
)
|
||||
|
||||
# # 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()
|
||||
# )
|
||||
|
||||
# Basic theme
|
||||
g <- g +
|
||||
hrbrthemes::theme_ipsum() +
|
||||
waffle::theme_enhance_waffle()
|
||||
# g <- g +
|
||||
# hrbrthemes::theme_ipsum() #+
|
||||
# waffle::theme_enhance_waffle()
|
||||
|
||||
# Add theme
|
||||
g <- g + theme
|
||||
|
|
@ -93,82 +76,3 @@ 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,))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue