Apply unique size aesthetic to each chart in a faceted ggplot chart

Could someone teach me how to apply a unique size aesthetic to each chart in my faceted chart? My code below gets me the chart below.

df <- structure(list(submission_type = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L), levels = c("Advance Account/KK Date Extension", 
"Award Notice", "Cost Transfer", "Interim Financial Report", 
"Just-In-Time", "No Cost Extension", "Prior Approval Request/PI Change", 
"Progress Report", "Proposal", "Rebudget/Budget Realignment", 
"Residual Transfer", "Subaward"), class = "factor"), submission_type_numeric = c(2, 
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 7, 7, 7, 
7, 7, 7, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 
9, 9, 6, 6, 6, 1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 12, 12, 12, 
12, 12, 4, 4, 4, 4, 4, 4, 4), fiscal_year = c(2017, 2016, 2019, 
2018, 2020, 2022, 2020, 2019, 2021, 2016, 2017, 2018, 2022, 2020, 
2022, 2017, 2019, 2016, 2018, 2017, 2018, 2021, 2020, 2019, 2022, 
2017, 2018, 2020, 2022, 2017, 2018, 2016, 2022, 2019, 2020, 2016, 
2019, 2018, 2020, 2017, 2022, 2018, 2020, 2022, 2016, 2020, 2018, 
2019, 2017, 2022, 2020, 2019, 2018, 2017, 2022, 2016, 2020, 2018, 
2019, 2017, 2016, 2021, 2020, 2017, 2018, 2016, 2019, 2022), 
    n_entries = c(18L, 25L, 25L, 27L, 27L, 61L, 116L, 121L, 144L, 
    154L, 156L, 160L, 179L, 40L, 62L, 76L, 96L, 103L, 124L, 5L, 
    11L, 11L, 13L, 19L, 21L, 1L, 1L, 1L, 2L, 19L, 24L, 26L, 26L, 
    29L, 33L, 2L, 2L, 4L, 4L, 7L, 14L, 1L, 2L, 3L, 104L, 147L, 
    154L, 154L, 155L, 161L, 23L, 38L, 40L, 45L, 54L, 56L, 1L, 
    7L, 7L, 17L, 23L, 12L, 26L, 30L, 32L, 34L, 37L, 38L), median_duration_sec = structure(c(256620, 
    163560, 157320, 188100, 591079, 265824, 772430, 2424540, 
    22844774, 677100, 2310150, 1269720, 2508301, 429005, 614270.5, 
    241470, 262320, 210360, 404580, 1706700, 100920, 51513636, 
    152808, 400470, 82642, 112560, 344280, 776626, 56472.5, 409980, 
    348780, 611520, 954329, 334049, 514352, 957480, 2357820, 
    1285110, 825986, 2708940, 1079354, 59340, 1060295.5, 77575, 
    606060, 948402, 596130, 1042620, 508920, 784389.5, 626998, 
    678960, 671400, 506940, 530208, 494280, 2560413, 4544250, 
    11186040, 3702000, 1177860, 12608321, 5644122, 4775040, 4168980, 
    2355180, 5167320, 8125365), class = "difftime", units = "secs"), 
    median_duration_days = c(2.97013888888889, 1.89305555555556, 
    1.82083333333333, 2.17708333333333, 6.84119212962963, 3.07666666666667, 
    8.94016203703704, 28.0618055555556, 264.407106481481, 7.83680555555556, 
    26.7378472222222, 14.6958333333333, 29.0312615740741, 4.96533564814815, 
    7.10961226851852, 2.79479166666667, 3.03611111111111, 2.43472222222222, 
    4.68263888888889, 19.7534722222222, 1.16805555555556, 596.222638888889, 
    1.76861111111111, 4.63506944444444, 0.95650462962963, 1.30277777777778, 
    3.98472222222222, 8.98872685185185, 0.653616898148148, 4.74513888888889, 
    4.03680555555556, 7.07777777777778, 11.045474537037, 3.86630787037037, 
    5.95314814814815, 11.0819444444444, 27.2895833333333, 14.8739583333333, 
    9.56002314814815, 31.3534722222222, 12.4925231481481, 0.686805555555556, 
    12.2719386574074, 0.897858796296296, 7.01458333333333, 10.976875, 
    6.89965277777778, 12.0673611111111, 5.89027777777778, 9.07858217592593, 
    7.2569212962963, 7.85833333333333, 7.77083333333333, 5.86736111111111, 
    6.13666666666667, 5.72083333333333, 29.6344097222222, 52.5954861111111, 
    129.468055555556, 42.8472222222222, 13.6326388888889, 145.929641203704, 
    65.3254861111111, 55.2666666666667, 48.2520833333333, 27.2590277777778, 
    59.8069444444444, 94.0435763888889), avg_n_entries = c(30.5555555555556, 
    30.5555555555556, 30.5555555555556, 30.5555555555556, 30.5555555555556, 
    30.5555555555556, 133.888888888889, 133.888888888889, 133.888888888889, 
    133.888888888889, 133.888888888889, 133.888888888889, 133.888888888889, 
    69.7777777777778, 69.7777777777778, 69.7777777777778, 69.7777777777778, 
    69.7777777777778, 69.7777777777778, 11.5714285714286, 11.5714285714286, 
    11.5714285714286, 11.5714285714286, 11.5714285714286, 11.5714285714286, 
    1.6, 1.6, 1.6, 1.6, 22.2222222222222, 22.2222222222222, 22.2222222222222, 
    22.2222222222222, 22.2222222222222, 22.2222222222222, 7.66666666666667, 
    7.66666666666667, 7.66666666666667, 7.66666666666667, 7.66666666666667, 
    7.66666666666667, 2.2, 2.2, 2.2, 131.125, 131.125, 131.125, 
    131.125, 131.125, 131.125, 35.1111111111111, 35.1111111111111, 
    35.1111111111111, 35.1111111111111, 35.1111111111111, 35.1111111111111, 
    8.5, 8.5, 8.5, 8.5, 8.5, 28.8888888888889, 28.8888888888889, 
    28.8888888888889, 28.8888888888889, 28.8888888888889, 28.8888888888889, 
    28.8888888888889), point_size = c(-1.39506172839506, -0.617283950617284, 
    -0.617283950617284, -0.395061728395062, -0.395061728395062, 
    3.38271604938272, -1.98765432098765, -1.4320987654321, 1.12345679012346, 
    2.23456790123457, 2.45679012345679, 2.90123456790124, 5.01234567901235, 
    -3.30864197530864, -0.864197530864197, 0.691358024691359, 
    2.91358024691358, 3.69135802469136, 6.02469135802469, -0.938775510204082, 
    -0.0816326530612245, -0.0816326530612245, 0.204081632653061, 
    1.06122448979592, 1.3469387755102, -0.12, -0.12, -0.12, 0.08, 
    -0.358024691358025, 0.197530864197531, 0.419753086419753, 
    0.419753086419753, 0.753086419753087, 1.19753086419753, -0.62962962962963, 
    -0.62962962962963, -0.407407407407407, -0.407407407407407, 
    -0.0740740740740741, 0.703703703703704, -0.24, -0.04, 0.16, 
    -3.390625, 1.984375, 2.859375, 2.859375, 2.984375, 3.734375, 
    -1.34567901234568, 0.320987654320987, 0.54320987654321, 1.09876543209876, 
    2.09876543209876, 2.32098765432099, -0.9375, -0.1875, -0.1875, 
    1.0625, 1.8125, -1.87654320987654, -0.320987654320988, 0.123456790123457, 
    0.345679012345679, 0.567901234567901, 0.901234567901235, 
    1.01234567901235)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -68L), groups = structure(list(
    submission_type = structure(1:12, levels = c("Advance Account/KK Date Extension", 
    "Award Notice", "Cost Transfer", "Interim Financial Report", 
    "Just-In-Time", "No Cost Extension", "Prior Approval Request/PI Change", 
    "Progress Report", "Proposal", "Rebudget/Budget Realignment", 
    "Residual Transfer", "Subaward"), class = "factor"), .rows = structure(list(
        1:6, 7:13, 14:19, 20:25, 26:29, 30:35, 36:41, 42:44, 
        45:50, 51:56, 57:61, 62:68), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))


df %>% 
  ggplot(aes(x = fiscal_year, y = median_duration_days)) + 
  geom_segment(aes(x=fiscal_year, xend=fiscal_year, y=0, yend=median_duration_days)) +
  geom_point(aes(size = n_entries), color = "grey80", fill = "grey20") +
  geom_point(aes(size = n_entries), pch = 21) +
  scale_x_continuous(breaks = seq(2016, 2022, 1),
                     labels = c("FY16", "FY17", "FY18", "FY19", 
                                "FY20", "FY21", "FY22"),
                     minor_breaks = NULL) +
  facet_wrap(~ submission_type %>% fct_reorder(submission_type_numeric), scales = "free") +
  coord_flip() +
  labs(x = "",
       y = "Time in days",
       title = "Median number of days to complete each process",
       subtitle = "Point sizes represent total # of entries that FY",
       size = "# of entries") + 
  theme(legend.position = "bottom")

But what I really want is for each chart to look like this with its unique sized dots/legend applied...and then I'll remove each legend with legend.position = "none"

I'm not sure how to accomplish this within a faceted output, but another approach would be to create a layout of individual plots with the patchwork package. The example below turns your plot code into a function, maps through each submission type, and finally organizes all of the plots into one layout. I commented out certain sections of the code you provided and added "# NEW" at the end of newly added lines.

library(tidyverse)
library(patchwork)

# function to create each plot
create_plot = function(i) {
  df %>% 
    filter(submission_type == i) %>% # NEW
    ggplot(aes(x = fiscal_year, y = median_duration_days)) + 
    geom_segment(aes(x=fiscal_year, xend=fiscal_year, y=0, yend=median_duration_days)) +
    geom_point(aes(size = n_entries), color = "grey80", fill = "grey20") +
    geom_point(aes(size = n_entries), pch = 21) +
    scale_x_continuous(breaks = seq(2016, 2022, 1),
                       labels = c("FY16", "FY17", "FY18", "FY19", 
                                  "FY20", "FY21", "FY22"),
                       minor_breaks = NULL) +
    # facet_wrap(~ submission_type %>% fct_reorder(submission_type_numeric), scales = "free") +
    coord_flip() +
    labs(x = "",
         y = "Time in days",
         title = i, # NEW
         # subtitle = "Point sizes represent total # of entries that FY",
         size = "# of entries"
         ) + 
    theme(legend.position = "bottom",
          plot.title = element_text(size = 9), # NEW
          axis.title = element_text(size = 7), # NEW
          legend.key.height = unit(0.15, 'cm'), # NEW
          legend.title = element_text(size = 7), # NEW
          legend.text = element_text(size = 6) # NEW
          )
}

# extract and order each submission type
submission_types = df$submission_type %>% fct_reorder(df$submission_type_numeric) %>% sort() %>% unique()

# create list of plots by mapping through each submission type
myplots = map(submission_types, create_plot)

# organize all plots in one layout
wrap_plots(myplots, ncol = 3) +
  plot_annotation(title = 'Median number of days to complete each process', 
                  subtitle = 'Point sizes represent total # of entries that FY')

Created on 2023-07-27 with reprex v2.0.2

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.