Karamanis labels plots with care.
library(tidyverse)
library(ggcirclepack)
pride_index <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-06-11/pride_index.csv')
## Rows: 238 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): campus_name, campus_location, community_type
## dbl (2): rating, students
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
comm_types <- c("large urban city",
"medium city",
"small city",
"small town",
"very small town",
"rural community")
pride_it <- pride_index %>%
# fix a typo
mutate(campus_location = str_replace(campus_location, "Swarrthmore", "Swarthmore")) %>%
mutate(community_type = fct_relevel(community_type, comm_types)) %>%
mutate(state = str_sub(campus_location, -2, -1)) %>%
mutate(campus_name = str_replace(campus_name, "University", "U"))
# Plot to get text labels
l <- ggplot(pride_it, aes(id = campus_name, area = students, fill = rating == 5)) +
geom_circlepack(linewidth = 0.2) +
geom_circlepack_text() +
coord_fixed() +
facet_wrap(vars(community_type))
# Get text labels and "add" community type for facets
cpl <- layer_data(plot = l, i = 2) %>%
mutate(
community_type = comm_types[PANEL],
community_type = fct_relevel(community_type, comm_types)
) %>%
group_by(community_type) %>%
mutate(min_y = min(y)) %>%
ungroup()
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
f1 <- "Times"# "Graphik"
f1b <- "Times"# "Graphik Compact"
f2 <- "Times"#"Publico Headline"
ggplot() +
geom_text(data = cpl %>%
distinct(community_type, min_y),
aes(x = 0, min_y - 270, label = community_type),
family = f1, fontface = "bold", size = 4, color = "#ED702D") +
geom_segment(data = cpl %>%
distinct(community_type, min_y),
aes(x = 0, xend = 0,
y = min_y, yend = min_y - 180),
linewidth = 0.2, color = "#ED702D") +
geom_circlepack(data = pride_it,
aes(id = campus_name,
area = students,
fill = rating == 5),
linewidth = 0.2, color = "grey99", linewidth = 0.1) +
ggrepel::geom_text_repel(data =
cpl %>% filter(fill == "#00BFC4"),
aes(x, y, label = str_wrap(label, 20)),
family = f1b,
bg.color = alpha("grey99", 1),
color = "grey10", size = 3,
lineheight = 0.9, fontface = "bold",
bg.r = 0.1, segment.color = "white") +
scale_fill_manual(values = c("grey70", "#64157B")) +
coord_fixed(clip = "off", expand = FALSE) +
facet_wrap(vars(community_type), strip.position = "bottom") +
labs(
title = "The most LGBTQ-friendly campuses",
subtitle = "Out of the 238 colleges and universities in the Campus Pride Index's database, 47 have a <span style='color:#64157B'>**top rating of 5**</span>.<br>Two-thirds of them are in small and medium-sized cities. Circle size represents the number of students.",
caption = "Source: Campus Pride Index · Graphic: Georgios Karamanis"
) +
theme_void(base_family = f1) +
theme(
legend.position = "none",
plot.background = element_rect(fill = "grey99", color = NA),
plot.margin = margin(10, 10, 10, 10),
strip.text = element_blank(),
# strip.text = element_text(size = 12, face = "bold"),
plot.title = element_text(family = f2, size = 20, face = "bold", color = "#ED702D"),
plot.subtitle = ggtext::element_markdown(size = 13,
lineheight = 1.1,
margin = margin(5, 0, 20, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
## Warning: Duplicated aesthetics after name standardisation: linewidth
## Warning in geom_circlepack(data = pride_it, aes(id = campus_name, area =
## students, : Ignoring unknown aesthetics: area
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Update: aes(render = render_condition)
So we think this is a pretty general problem, so updating ggcirclepack to allow for conditional label of circles w/ aes(render = render_condition)
pride_it %>% head()
## # A tibble: 6 × 6
## campus_name campus_location rating students community_type state
## <chr> <chr> <dbl> <dbl> <fct> <chr>
## 1 U of Maryland, College P… College Park, … 5 37952 large urban c… MD
## 2 U of Michigan-Dearborn Dearborn, MI 3 9000 medium city MI
## 3 SUNY Westchester Communi… Valhalla, NY 4 13000 very small to… NY
## 4 U at Buffalo Buffalo, NY 3.5 29850 medium city NY
## 5 Slippery Rock U Slippery Rock,… 4 8500 rural communi… PA
## 6 Emory U Atlanta, GA 4.5 14769 large urban c… GA
pride_it %>%
mutate(render = rating == 5) %>%
rename(id = campus_name,
area = students,
fill = rating) %>%
ggcirclepack:::StatCirclepackcenter$compute_panel() %>%
tibble()
## Warning: Unknown or uninitialised column: `slice`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## # A tibble: 47 × 10
## x y radius id fill render area within prop percent
## <dbl> <dbl> <dbl> <chr> <dbl> <lgl> <dbl> <dbl> <dbl> <dbl>
## 1 -50.0 0 50.0 Adelphi U 5 TRUE 7859 1 2.73e-3 0
## 2 32.3 56.9 33.6 Augsburg U 5 TRUE 3537 1 1.23e-3 0
## 3 -74.2 -341. 45.5 Elon U 5 TRUE 6500 1 2.26e-3 0
## 4 -161. 403. 112. George Mason U 5 TRUE 39150 1 1.36e-2 1
## 5 302. -152. 19.5 Guilford Coll… 5 TRUE 1200 1 4.17e-4 0
## 6 -438. 124. 55.1 Hofstra U 5 TRUE 9533 1 3.31e-3 0
## 7 482. -128. 126. Indiana U, Bl… 5 TRUE 49695 1 1.73e-2 2
## 8 446. -294. 43.7 Ithaca College 5 TRUE 6000 1 2.08e-3 0
## 9 -466. -108. 79.8 Kansas State U 5 TRUE 20000 1 6.94e-3 1
## 10 210. -509. 94.4 Kent State U 5 TRUE 28000 1 9.72e-3 1
## # ℹ 37 more rows
ggplot(pride_it) +
aes(id = campus_name) +
aes(area = students) +
aes(fill = rating == 5) +
geom_circlepack(linewidth = 0.2, color = "grey99") +
# aes() +
ggcirclepack:::geom_circlepack_text(
aes(render = rating == 5,
# label = after_stat(auto_label) %>% # auto label is not in StatCirclepack
# str_wrap(20)
),
lineheight = .8) +
facet_wrap(facets = vars(community_type)) +
scale_size(range = c(2,3))
## Warning in ggcirclepack:::geom_circlepack_text(aes(render = rating == 5, :
## Ignoring unknown aesthetics: render
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
last_plot() +
aes(label = campus_name)
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
ggplot(pride_it) +
aes(id = campus_name) +
aes(area = students) +
aes(fill = rating == 5) +
geom_circlepack(linewidth = 0.2, color = "grey99") +
ggcirclepack:::stat_circlepack_center(
aes(render = rating == 5,
label = after_stat(auto_label %>% str_wrap(20))
),
geom = ggrepel::GeomTextRepel,
lineheight = .8) +
facet_wrap(facets = vars(community_type)) +
scale_size(range = c(2,3))
## Warning in ggcirclepack:::stat_circlepack_center(aes(render = rating == :
## Ignoring unknown aesthetics: render
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Error in `ggcirclepack:::stat_circlepack_center()`:
## ! Problem while mapping stat to aesthetics.
## ℹ Error occurred in the 2nd layer.
## Caused by error:
## ! object 'auto_label' not found
ggplot(pride_it) +
aes(id = campus_name) +
aes(area = students) +
aes(fill = rating == 5) +
geom_circlepack(linewidth = 0.2, color = "grey99") +
geom_circlepack_text() +
facet_wrap(facets = vars(community_type)) +
scale_size(range = c(-2,3)) +
aes(label = str_wrap(after_stat(id), 10)) +
aes(size = stage(after_stat = area, after_scale = ifelse(size < 1, 0, size)))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Failed to apply `after_scale()` modifications to legend
## Caused by error:
## ! object 'size' not found
last_plot() +
aes(area = 1/students) +
scale_size(range = c(-2,1)) +
aes(size = stage(after_stat = area, after_scale = ifelse(size < .5, 0, size)))
## Scale for size is already present.
## Adding another scale for size, which will replace the existing scale.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Failed to apply `after_scale()` modifications to legend
## Caused by error:
## ! object 'size' not found
pride_index <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-06-11/pride_index.csv')
## Rows: 238 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): campus_name, campus_location, community_type
## dbl (2): rating, students
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ggplot(pride_it) +
aes(id = campus_name) +
aes(area = students) +
aes(fill = rating == 5) +
geom_circlepack(linewidth = 0.2, color = "grey99") +
aes(label = str_wrap(after_stat(id), 10)) +
geom_circlepack_text() +
scale_size(range = c(0,3)) +
aes(size = rating) +
facet_wrap(facets = vars(community_type))
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
## Warning: Unknown or uninitialised column: `wt`.
## Warning: Unknown or uninitialised column: `within`.
circlepa
## Error in eval(expr, envir, enclos): object 'circlepa' not found
ggcirclepack:::StatCirclepackcenter$default_aes
## Aesthetic mapping:
## * `group` -> `after_stat(id)`
## * `size` -> `after_stat(area)`
## * `label` -> `after_stat(id)`
ggcirclepack:::StatCirclepackcenter$compute_panel
## <ggproto method>
## <Wrapper function>
## function (...)
## compute_panel(...)
##
## <Inner function (f)>
## function (data, scales, fun = sum)
## {
## if (is.null(data$slice)) {
## data$slice <- TRUE
## }
## data <- data %>% dplyr::filter(.data$slice)
## grp_cols <- c("id", "fill", "alpha", "colour", "group", "linewidth",
## "label", "size", "linetype", "render")
## data <- data %>% group_by(group_by(pick(any_of(grp_cols))))
## if (is.null(data$area)) {
## data <- mutate(data, area = 1)
## }
## if (is.null(data$wt)) {
## data$wt <- 1
## }
## data <- data %>% summarize(area = fun(.data$area * .data$wt),
## .groups = "drop")
## data <- data %>% arrange(id)
## if (is.null(data$within)) {
## data$within <- 1
## }
## data <- data %>% group_by(.data$within) %>% mutate(prop = .data$area/sum(.data$area)) %>%
## mutate(percent = round(.data$prop * 100))
## data <- data %>% pull(area) %>% packcircles::circleProgressiveLayout(sizetype = "area") %>%
## cbind(data)
## if (!is.null(data$render)) {
## data <- data %>% filter(.data$render)
## }
## data
## }