```{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 <- paletteer::paletteer_d("ButterflyColors::fountainea_ryphea")[c(1)]
### |- titles and caption ----
# icons
tt <- str_glue("#TidyTuesday: { 2024 } Week { 41 } • Source: NPSpecies - The National Park Service biodiversity database<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("Radar Charts of Species Categories by National Park")
subtitle_text <- str_glue("Comparison of species distributions across U.S. national parks")
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_minimal(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),
axis.title.x = element_text(margin = margin(10, 0, 0, 0), size = rel(1.1),
color = text_col, family = "text", face = "bold", hjust = 0.5),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(color = text_col, family = "text", size = rel(0.9)),
axis.ticks.x = element_line(color = text_col),
)
### |- plot function ----
# Function to create radar chart and save as PNG
create_and_save_radar_plot <- function(data, park_name) {
# Define maximum and minimum values for the radar chart
max_values <- rep(max(data), ncol(data))
min_values <- rep(0, ncol(data))
# Combine the max, min, and park data to create the radar chart data frame
plot_data <- as.data.frame(rbind(max_values, min_values, data))
colnames(plot_data) <- names(data)
rownames(plot_data) <- c("Max", "Min", park_name)
# Define the file path to save the radar chart
temp_path <- here("2024/Week_41/")
if (!dir.exists(temp_path)) {
dir.create(temp_path, recursive = TRUE)
}
file_path <- file.path(temp_path, paste0("radar_plot_", gsub(" ", "_", park_name), ".png"))
# Close any open devices
while (!is.null(dev.list())) dev.off()
# Create and save the radar chart as a PNG
png(filename = file_path, width = 400, height = 400)
fmsb::radarchart(plot_data,
axistype = 1, # Axis type configuration
title = park_name, # Title for the radar chart
pcol = col_palette, # Line color for the polygon
pfcol = scales::alpha(col_palette, 0.25), # Fill color for the polygon with transparency
plty = 1, # Line type for the polygon
cglcol = bkg_col, # Color of the grid lines
cglty = 1, # Type of the grid lines
cglwd = 0.8, # Width of the grid lines
axislabcol = bkg_col, # Color of the axis labels
cex.axis = 1.2, # Increase axis text size
cex.main = 1.5 # Increase title text size
)
dev.off()
return(file_path)
}
```