replace_data <-function() { dplyr::tibble(labels, numbers, unit, colours) |># Getting everything to stay in the right order! dplyr::mutate(labels =factor(labels, levels = labels, ordered =TRUE))}theme_barsberight <-function() {# We looked at this earlier}make_graphs <-function(df =replace_data()) {# Create and assemble 5 plots,# making use of theme_barsberight()# for all of them!}
Parameterising the unknown
The glorious gory details
What if the sum isn’t 100?
Easy, fill in the rest with NAs!
What if it’s greater than 100…?
Wait, what if it isn’t a percentage?
make_graphs <-function(df =replace_data()) { empty <- ggplot2::ggplot(df, ggplot2::aes(x = labels,y = numbers)) + ggplot2::labs(title =toupper("It looked\nlike this")) +theme_barsberight() bars <- ggplot2::ggplot(df, ggplot2::aes(x = labels,y = numbers,fill = colours),colour ="#FFFFFF") + ggplot2::geom_bar(stat ="identity",linewidth =1) + ggtext::geom_textbox(ggplot2::aes(label =paste0(labels,"<span style=font-size:16pt><br>", numbers, unit,"</span>")),vjust =0,size =3.5,family ="Trebuchet MS",halign =0.5,hjust =0.5,lineheight =1.6,fill =NA,box.colour =NA) + ggplot2::labs(title =toupper("But should have looked\nmore like this")) + ggplot2::scale_fill_identity() + ggplot2::scale_y_continuous(expand = ggplot2::expansion(c(0.05, 0.25))) +theme_barsberight()# Setting up the waffle data# If the unit is 100%if(unique(df$unit) =="%") {if(sum(df$numbers, na.rm =TRUE) ==100) { df <- df |> dplyr::mutate(waffle_proportions = numbers) } elseif(sum(df$numbers, na.rm =TRUE) !=100) {# if the total is less than 100, show unaccounted onesif(sum(df$numbers, na.rm =TRUE) <100) { df <- df |> dplyr::bind_rows(dplyr::tibble(labels =NA,numbers =100-sum(df$numbers),unit ="%",colours ="grey85")) |> dplyr::mutate(waffle_proportions = numbers)# If it's more than 100, bring it back to 100 } elseif(sum(df$numbers, na.rm =TRUE) >100) { df <- df |> dplyr::mutate(waffle_proportions = janitor::round_half_up(numbers/sum(numbers, na.rm =TRUE) *100)) } }# If its not %, we just need proportions, regardless of whether the total equals 100 } else { df <- df |> dplyr::mutate(waffle_proportions = janitor::round_half_up(numbers/sum(numbers, na.rm =TRUE) *100)) } x_coords <- y_coords <- waffle_colours <-c()for(prop in df$waffle_proportions) { x_coords <-rep(1:10, 10) y_coords <-sort(rep(1:10, 10)) waffle_colours <-c(waffle_colours, rep(dplyr::filter(df, waffle_proportions == prop) |> dplyr::select(colours) |> dplyr::pull(), prop)) } waffle_data <- dplyr::tibble(x_coords, y_coords, waffle_colours) waffle <- ggplot2::ggplot(waffle_data, ggplot2::aes(fill = waffle_colours,x = x_coords,y = y_coords)) + ggplot2::geom_point(shape =22,colour ="#FFFFFF",stroke =1,size =6) + ggplot2::labs(title =toupper("\nlike this")) + ggplot2::scale_fill_identity() + ggplot2::coord_fixed() + ggplot2::scale_y_continuous(expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_x_continuous(expand = ggplot2::expansion(c(0.1, 0.1))) +theme_barsberight() donut <- ggplot2::ggplot(df, ggplot2::aes(x =1,y = numbers, fill = labels)) + ggplot2::geom_bar(stat ="identity", position ="stack",linewidth =1, colour ="#FFFFFF",show.legend =FALSE) + ggplot2::labs(title =toupper(" - It could also have looked - \nOr this")) + ggplot2::scale_fill_manual(values = df$colours, na.value ="grey85") + ggplot2::scale_x_continuous(limits =c(-0.25, 1.5)) + ggplot2::coord_polar(theta ="y", direction =-1) +theme_barsberight() x_coords <- y_coords <- macaron_colours <-c()for(num in df$numbers) { x_coords <-c(x_coords, runif(num, -1, 1)) y_coords <-c(y_coords, runif(num, -1, 1)) macaron_colours <-c(macaron_colours, rep(dplyr::filter(df, numbers == num) |> dplyr::select(colours) |> dplyr::pull(), num)) } macaron_data <- dplyr::tibble(x_coords, y_coords, macaron_colours) macarons <- ggplot2::ggplot(macaron_data, ggplot2::aes(x = x_coords,y = y_coords, fill = macaron_colours)) + ggplot2::geom_point(shape =21, colour ="#FFFFFF",size =6, alpha =0.9,stroke =1) + ggplot2::labs(title =toupper("\nOr this"),caption ="#BarsBeRight\nwww.cararthompson.com/apps/barsberight") + ggplot2::scale_y_continuous(limits =c(-1, 1), expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_x_continuous(limits =c(-1, 1), expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_fill_identity() + ggplot2::coord_fixed() +theme_barsberight()library(patchwork) row1 <- empty + bars row2 <- waffle + donut + macarons row1 / row2 +plot_layout(heights =c(3, 2)) &plot_annotation(theme = ggplot2::theme(plot.background = ggplot2::element_rect(color ='#FFFFFF50', linewidth =0, fill ="#FFFFFF70"),plot.margin = ggplot2::unit(c(5, 5, 5, 5), "pt")))}
Parameterising the unknown
The glorious gory details
What if they input the same colour twice?
You mean like in the original?
Why won’t the waffle plot package load?
Create a waffle from scratch!
make_graphs <-function(df =replace_data()) { empty <- ggplot2::ggplot(df, ggplot2::aes(x = labels,y = numbers)) + ggplot2::labs(title =toupper("It looked\nlike this")) +theme_barsberight() bars <- ggplot2::ggplot(df, ggplot2::aes(x = labels,y = numbers,fill = colours),colour ="#FFFFFF") + ggplot2::geom_bar(stat ="identity",linewidth =1) + ggtext::geom_textbox(ggplot2::aes(label =paste0(labels,"<span style=font-size:16pt><br>", numbers, unit,"</span>")),vjust =0,size =3.5,family ="Trebuchet MS",halign =0.5,hjust =0.5,lineheight =1.6,fill =NA,box.colour =NA) + ggplot2::labs(title =toupper("But should have looked\nmore like this")) + ggplot2::scale_fill_identity() + ggplot2::scale_y_continuous(expand = ggplot2::expansion(c(0.05, 0.25))) +theme_barsberight()# Setting up the waffle data# If the unit is 100%if(unique(df$unit) =="%") {if(sum(df$numbers, na.rm =TRUE) ==100) { df <- df |> dplyr::mutate(waffle_proportions = numbers) } elseif(sum(df$numbers, na.rm =TRUE) !=100) {# if the total is less than 100, show unaccounted onesif(sum(df$numbers, na.rm =TRUE) <100) { df <- df |> dplyr::bind_rows(dplyr::tibble(labels =NA,numbers =100-sum(df$numbers),unit ="%",colours ="grey85")) |> dplyr::mutate(waffle_proportions = numbers)# If it's more than 100, bring it back to 100 } elseif(sum(df$numbers, na.rm =TRUE) >100) { df <- df |> dplyr::mutate(waffle_proportions = janitor::round_half_up(numbers/sum(numbers, na.rm =TRUE) *100)) } }# If its not %, we just need proportions, regardless of whether the total equals 100 } else { df <- df |> dplyr::mutate(waffle_proportions = janitor::round_half_up(numbers/sum(numbers, na.rm =TRUE) *100)) } x_coords <- y_coords <- waffle_colours <-c()for(prop in df$waffle_proportions) { x_coords <-rep(1:10, 10) y_coords <-sort(rep(1:10, 10)) waffle_colours <-c(waffle_colours, rep(dplyr::filter(df, waffle_proportions == prop) |> dplyr::select(colours) |> dplyr::pull(), prop)) } waffle_data <- dplyr::tibble(x_coords, y_coords, waffle_colours) waffle <- ggplot2::ggplot(waffle_data, ggplot2::aes(fill = waffle_colours,x = x_coords,y = y_coords)) + ggplot2::geom_point(shape =22,colour ="#FFFFFF",stroke =1,size =6) + ggplot2::labs(title =toupper("\nlike this")) + ggplot2::scale_fill_identity() + ggplot2::coord_fixed() + ggplot2::scale_y_continuous(expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_x_continuous(expand = ggplot2::expansion(c(0.1, 0.1))) +theme_barsberight() donut <- ggplot2::ggplot(df, ggplot2::aes(x =1,y = numbers, fill = labels)) + ggplot2::geom_bar(stat ="identity", position ="stack",linewidth =1, colour ="#FFFFFF",show.legend =FALSE) + ggplot2::labs(title =toupper(" - It could also have looked - \nOr this")) + ggplot2::scale_fill_manual(values = df$colours, na.value ="grey85") + ggplot2::scale_x_continuous(limits =c(-0.25, 1.5)) + ggplot2::coord_polar(theta ="y", direction =-1) +theme_barsberight() x_coords <- y_coords <- macaron_colours <-c()for(num in df$numbers) { x_coords <-c(x_coords, runif(num, -1, 1)) y_coords <-c(y_coords, runif(num, -1, 1)) macaron_colours <-c(macaron_colours, rep(dplyr::filter(df, numbers == num) |> dplyr::select(colours) |> dplyr::pull(), num)) } macaron_data <- dplyr::tibble(x_coords, y_coords, macaron_colours) macarons <- ggplot2::ggplot(macaron_data, ggplot2::aes(x = x_coords,y = y_coords, fill = macaron_colours)) + ggplot2::geom_point(shape =21, colour ="#FFFFFF",size =6, alpha =0.9,stroke =1) + ggplot2::labs(title =toupper("\nOr this"),caption ="#BarsBeRight\nwww.cararthompson.com/apps/barsberight") + ggplot2::scale_y_continuous(limits =c(-1, 1), expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_x_continuous(limits =c(-1, 1), expand = ggplot2::expansion(c(0.1, 0.1))) + ggplot2::scale_fill_identity() + ggplot2::coord_fixed() +theme_barsberight()library(patchwork) row1 <- empty + bars row2 <- waffle + donut + macarons row1 / row2 +plot_layout(heights =c(3, 2)) &plot_annotation(theme = ggplot2::theme(plot.background = ggplot2::element_rect(color ='#FFFFFF50', linewidth =0, fill ="#FFFFFF70"),plot.margin = ggplot2::unit(c(5, 5, 5, 5), "pt")))}