```{r}
#| label: params
### |- plot aesthetics ----
bkg_col <- colorspace::lighten('#f7f5e9', 0.05)
title_col <- "gray20"
subtitle_col <- "gray20"
caption_col <- "gray30"
text_col <- "gray20"
col_palette <- NatParksPalettes::natparks.pals(name = 'CraterLake', n = 3, type = "discrete")
col_palette <- colorspace::lighten(col_palette, 0.1)
### |- titles and caption ----
# icons
tt <- str_glue("#TidyTuesday: { 2024 } Week { 38 } • Source: shakespeare.mit.edu (via github.com/nrennie/shakespeare<br>")
li <- str_glue("<span style='font-family:fa6-brands'></span>")
gh <- str_glue("<span style='font-family:fa6-brands'></span>")
mn <- str_glue("<span style='font-family:fa6-brands'></span>")
# text
title_text <- str_glue("Character Interaction Networks in Shakespeare's Plays")
subtitle_text <- str_glue("Visualizing character exchanges across different scenes and acts")
caption_text <- str_glue("{tt} {li} stevenponce • {mn} @sponce1(graphic.social) {gh} poncest • #rstats #ggplot2")
### |- fonts ----
font_add("fa6-brands", "fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf")
font_add_google("Oswald", regular.wt = 400, family = "title")
font_add_google("Merriweather Sans", regular.wt = 400, family = "subtitle")
font_add_google("Merriweather Sans", regular.wt = 400, family = "text")
font_add_google("Noto Sans", regular.wt = 400, family = "caption")
showtext_auto(enable = TRUE)
### |- plot theme ----
theme_set(theme_void(base_size = 14, base_family = "text"))
theme_update(
plot.title.position = "plot",
plot.caption.position = "plot",
legend.position = 'plot',
plot.background = element_rect(fill = bkg_col, color = bkg_col),
panel.background = element_rect(fill = bkg_col, color = bkg_col),
plot.margin = margin(t = 10, r = 20, b = 10, l = 20),
strip.text = element_textbox(size = rel(1),
face = 'bold',
color = text_col,
hjust = 0.5,
halign = 0.5,
r = unit(3, "pt"),
width = unit(6, "npc"),
padding = margin(2, 0, 2, 0),
margin = margin(3, 3, 3, 3),
fill = "transparent"),
panel.spacing = unit(1, 'lines')
)
### |- plot function ----
plot_character_network <- function(play_name, edges_data, node_color, edge_color) {
# Create igraph object for the play
g_play <- graph_from_data_frame(edges_data, directed = FALSE)
# Network plot
plot <- ggraph(g_play, layout = 'fr') +
geom_edge_link(aes(edge_alpha = n, edge_width = n), color = edge_color, show.legend = FALSE) + # Set edge color with alpha and width
geom_node_point(size = 5, color = node_color) + # Set node color
geom_node_text(aes(label = name), color = text_col, repel = TRUE, check_overlap = TRUE) +
scale_edge_width(range = c(0.5, 2.5)) +
theme_void() +
ggtitle(play_name) + # Add top-center title for the play
theme(
plot.title = element_text(size = rel(1.5), face = "bold", hjust = 0.5) # Centered title with bold font
)
return(plot)
}
```