require(pacman)
p_load(tidytuesdayR, magick, tidyverse, janitor, here, trashpanda, rnaturalearth, rnaturalearthdata, glue, ggtext, showtext, ggside)Astronomy Picture of the Day
Load Packages
Load Data
tuesdata <- tidytuesdayR::tt_load('2026-01-20')
apod <- tuesdata$apodData Cleaning
sun_mentions <- apod |>
mutate(
sun_mention =
str_detect(title, regex("(?<![A-Za-z])sun(?![A-Za-z])", ignore_case = TRUE)) |
str_detect(explanation, regex("(?<![A-Za-z])sun(?![A-Za-z])", ignore_case = TRUE)))
sun_mentions_cleaned <- sun_mentions |>
filter(sun_mention == TRUE)
# Compute % of month with sun mentioned
apod_solar <- sun_mentions |>
mutate(
month = month(date, label = TRUE, abbr = TRUE),
year = year(date)) |>
group_by(year, month) |>
summarise(
sun_share = mean(sun_mention, na.rm = TRUE),
.groups = "drop")
apod_solar_side_x <- sun_mentions |>
mutate(
month = month(date, label = TRUE, abbr = TRUE),
year = year(date)) |>
group_by(year) |>
summarise(
sun_share = sum(sun_mention, na.rm = TRUE),
.groups = "drop")
apod_solar_side_y <- sun_mentions |>
mutate(
month = month(date, label = TRUE, abbr = TRUE),
year = year(date)) |>
group_by(month) |>
summarise(
sun_share = sum(sun_mention, na.rm = TRUE),
.groups = "drop")Plot
plot <- ggplot(apod_solar, aes(year, fct_rev(month), fill = sun_share)) +
geom_tile(color = "black", linewidth = 0.15) +
geom_xsideline(
data = apod_solar_side_x,
aes(y = sun_share, x = year),
inherit.aes = FALSE,
linewidth = 1,
colour = "#FDB813",
alpha = 0.9) +
geom_ysidecol(
data = apod_solar_side_y,
aes(x = sun_share, y = month),
inherit.aes = FALSE,
fill = "#E3A93B",
alpha = 0.9
) +
scale_fill_gradient(
low = "#0B1026",
high = "#FDB813",
labels = scales::percent,
name = "Sun Mentions") +
labs(
x = "Year", y = "Month",
title = "When the Sun Appears in Astronomy Picture of the Day Posts",
subtitle = "Share of titles and explanations mentioning the Sun by month (2007–2025) \nEach tile = % of that month mentioning the Sun") +
theme_minimal(base_size = 12) +
scale_x_continuous(expand = c(0, 0)) +
scale_xsidey_continuous(breaks = c(100, 120, 140)) +
scale_ysidex_continuous(breaks = c(0, 100, 200)) +
theme(
panel.grid = element_blank(),
plot.background = element_rect(fill = "#070B1A", color = NA),
panel.background = element_rect(fill = "#070B1A", color = NA),
text = element_text(color = "#F4E9D8"),
axis.text = element_text(color = "#F4E9D8"),
legend.key = element_rect(fill = "#070B1A"),
plot.title = element_text(face = "bold"),
axis.title = element_text(face = "bold", size = 14),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13)
) +
add_caption_cwb(type = "plot",
include_data_source = TRUE,
data_source = "NASA Astronomy Picture of the Day Archive")
# Save and display images
current_dir <- dirname(knitr::current_input())
plot_name <- "apod_sun_mentions.png"
ggsave(plot = plot,
dpi = "screen",
width = 16,
height = 12,
device = ragg::agg_png,
filename = file.path(current_dir, plot_name))
img <- image_read(file.path(current_dir, plot_name))
img_card <- image_scale(img, "1200x675")
img_card <- image_extent(
img_card,
geometry = "1200x675",
gravity = "center"
)
# Save as card preview
image_write(img_card, path = file.path(current_dir, "preview.png"))
knitr::include_graphics(
file.path(current_dir, plot_name)
)
References
trashpanda::cite_packages(format = "rmd")Landis J (2025). ggside: Side Grammar Graphics. doi:10.32614/CRAN.package.ggside https://doi.org/10.32614/CRAN.package.ggside, R package version 0.4.1, https://CRAN.R-project.org/package=ggside.
Qiu Y, details. aotisSfAf (2024). showtext: Using Fonts More Easily in R Graphs. doi:10.32614/CRAN.package.showtext https://doi.org/10.32614/CRAN.package.showtext, R package version 0.9-7, https://CRAN.R-project.org/package=showtext.
Qiu Y, details. aotifSfAf (2020). showtextdb: Font Files for the ‘showtext’ Package. doi:10.32614/CRAN.package.showtextdb https://doi.org/10.32614/CRAN.package.showtextdb, R package version 3.0, https://CRAN.R-project.org/package=showtextdb.
Qiu Y, details. aotifSfAf (2024). sysfonts: Loading Fonts into R. doi:10.32614/CRAN.package.sysfonts https://doi.org/10.32614/CRAN.package.sysfonts, R package version 0.8.9, https://CRAN.R-project.org/package=sysfonts.
Wilke C, Wiernik B (2022). ggtext: Improved Text Rendering Support for ‘ggplot2’. doi:10.32614/CRAN.package.ggtext https://doi.org/10.32614/CRAN.package.ggtext, R package version 0.1.2, https://CRAN.R-project.org/package=ggtext.
Hester J, Bryan J (2024). glue: Interpreted String Literals. doi:10.32614/CRAN.package.glue https://doi.org/10.32614/CRAN.package.glue, R package version 1.8.0, https://CRAN.R-project.org/package=glue.
South A, Michael S, Massicotte P (2024). rnaturalearthdata: World Vector Map Data from Natural Earth Used in ‘rnaturalearth’. doi:10.32614/CRAN.package.rnaturalearthdata https://doi.org/10.32614/CRAN.package.rnaturalearthdata, R package version 1.0.0, https://CRAN.R-project.org/package=rnaturalearthdata.
Massicotte P, South A (2026). rnaturalearth: World Map Data from Natural Earth. doi:10.32614/CRAN.package.rnaturalearth https://doi.org/10.32614/CRAN.package.rnaturalearth, R package version 1.2.0, https://CRAN.R-project.org/package=rnaturalearth.
Baril C (????). trashpanda: Cole’s Personal Collection of R Functions, Themes, and Palettes. R package version 0.0.1, https://colebaril.github.io/trashpanda/.
Müller K (2025). here: A Simpler Way to Find Your Files. doi:10.32614/CRAN.package.here https://doi.org/10.32614/CRAN.package.here, R package version 1.0.2, https://CRAN.R-project.org/package=here.
Firke S (2024). janitor: Simple Tools for Examining and Cleaning Dirty Data. doi:10.32614/CRAN.package.janitor https://doi.org/10.32614/CRAN.package.janitor, R package version 2.2.1, https://CRAN.R-project.org/package=janitor.
Grolemund G, Wickham H (2011). “Dates and Times Made Easy with lubridate.” Journal of Statistical Software, 40(3), 1-25. https://www.jstatsoft.org/v40/i03/.
Wickham H (2025). forcats: Tools for Working with Categorical Variables (Factors). doi:10.32614/CRAN.package.forcats https://doi.org/10.32614/CRAN.package.forcats, R package version 1.0.1, https://CRAN.R-project.org/package=forcats.
Wickham H (2025). stringr: Simple, Consistent Wrappers for Common String Operations. doi:10.32614/CRAN.package.stringr https://doi.org/10.32614/CRAN.package.stringr, R package version 1.6.0, https://CRAN.R-project.org/package=stringr.
Wickham H, François R, Henry L, Müller K, Vaughan D (2023). dplyr: A Grammar of Data Manipulation. doi:10.32614/CRAN.package.dplyr https://doi.org/10.32614/CRAN.package.dplyr, R package version 1.1.4, https://CRAN.R-project.org/package=dplyr.
Wickham H, Henry L (2026). purrr: Functional Programming Tools. doi:10.32614/CRAN.package.purrr https://doi.org/10.32614/CRAN.package.purrr, R package version 1.2.1, https://CRAN.R-project.org/package=purrr.
Wickham H, Hester J, Bryan J (2025). readr: Read Rectangular Text Data. doi:10.32614/CRAN.package.readr https://doi.org/10.32614/CRAN.package.readr, R package version 2.1.6, https://CRAN.R-project.org/package=readr.
Wickham H, Vaughan D, Girlich M (2025). tidyr: Tidy Messy Data. doi:10.32614/CRAN.package.tidyr https://doi.org/10.32614/CRAN.package.tidyr, R package version 1.3.2, https://CRAN.R-project.org/package=tidyr.
Müller K, Wickham H (2026). tibble: Simple Data Frames. doi:10.32614/CRAN.package.tibble https://doi.org/10.32614/CRAN.package.tibble, R package version 3.3.1, https://CRAN.R-project.org/package=tibble.
Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4, https://ggplot2.tidyverse.org.
Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R, Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi K, Vaughan D, Wilke C, Woo K, Yutani H (2019). “Welcome to the tidyverse.” Journal of Open Source Software, 4(43), 1686. doi:10.21105/joss.01686 https://doi.org/10.21105/joss.01686.
Ooms J (2025). magick: Advanced Graphics and Image-Processing in R. doi:10.32614/CRAN.package.magick https://doi.org/10.32614/CRAN.package.magick, R package version 2.9.0, https://CRAN.R-project.org/package=magick.
Harmon J, Hughes E (2025). tidytuesdayR: Access the Weekly ‘TidyTuesday’ Project Dataset. doi:10.32614/CRAN.package.tidytuesdayR https://doi.org/10.32614/CRAN.package.tidytuesdayR, R package version 1.2.1, https://CRAN.R-project.org/package=tidytuesdayR.
Rinker TW, Kurkiewicz D (2018). pacman: Package Management for R. version 0.5.0, http://github.com/trinker/pacman.