Skip to content

Instantly share code, notes, and snippets.

@cararthompson
Last active June 2, 2025 15:32
Show Gist options
  • Select an option

  • Save cararthompson/3093eb13316ba6912218e3e12dac39a5 to your computer and use it in GitHub Desktop.

Select an option

Save cararthompson/3093eb13316ba6912218e3e12dac39a5 to your computer and use it in GitHub Desktop.
library(ggplot2)
# Get hold of the data ----
# |- Save this file
# https://static-content.springer.com/esm/art%3A10.1038%2Fs41588-022-01158-0/MediaObjects/41588_2022_1158_MOESM4_ESM.xlsx
# |- Read in the data
model_data <- readxl::read_xlsx(here::here(
"data/41588_2022_1158_MOESM4_ESM.xlsx"
))
# Here's my rendition of the "before" plot ----
model_data |>
ggplot() +
geom_abline() +
geom_point(aes(
x = h2.uncorrected,
y = h2.region_corrected,
colour = Category
)) +
geom_segment(aes(
x = h2.uncorrected,
xend = h2.uncorrected,
y = h2.region_corrected - h2_se.region_corrected,
yend = h2.region_corrected + h2_se.region_corrected,
colour = Category
)) +
geom_segment(aes(
x = h2.uncorrected - h2_se.uncorrected,
xend = h2.uncorrected + h2_se.uncorrected,
y = h2.region_corrected,
yend = h2.region_corrected,
colour = Category
)) +
theme_minimal()
# And here's a suggeted "after" plot ----
model_data |>
dplyr::arrange(h2.uncorrected) |>
dplyr::mutate(Trait = factor(Trait, levels = unique(Trait))) |>
dplyr::group_by(Category) |>
dplyr::mutate(mean_h2 = mean(h2.uncorrected, na.rm = TRUE)) |>
dplyr::ungroup() |>
dplyr::arrange(-mean_h2) |>
dplyr::mutate(Category = factor(Category, levels = unique(Category))) |>
ggplot() +
geom_segment(
aes(x = -Inf, xend = Inf, y = Trait, yend = Trait),
position = position_nudge(x = 0, y = -0.5),
colour = "white"
) +
annotate(
geom = "rect",
xmin = 0.06,
xmax = Inf,
ymin = -Inf,
ymax = Inf,
fill = "#a2c3bc",
alpha = 0.25
) +
annotate(
geom = "rect",
xmin = 0.138,
xmax = Inf,
ymin = -Inf,
ymax = Inf,
fill = "#a2c3bc",
alpha = 0.25
) +
geom_segment(
aes(
x = h2.uncorrected - h2_se.uncorrected,
xend = h2.uncorrected + h2_se.uncorrected,
y = Trait,
yend = Trait
),
position = position_nudge(x = 0, y = 0.1),
colour = "#5e656d",
alpha = 0.5,
linewidth = 1.5
) +
geom_segment(
aes(
x = h2.region_corrected - h2_se.region_corrected,
xend = h2.region_corrected + h2_se.region_corrected,
y = Trait,
yend = Trait
),
position = position_nudge(x = 0, y = -0.1),
colour = "#d26ea5",
alpha = 0.5,
linewidth = 1.5
) +
geom_segment(aes(
x = h2.uncorrected,
xend = h2.region_corrected,
y = Trait,
yend = Trait,
colour = dplyr::case_when(
h2.uncorrected > h2.region_corrected ~ "#5e656d",
TRUE ~ "#d26ea5"
)
)) +
geom_point(
aes(
y = Trait,
x = h2.region_corrected
),
shape = 21,
colour = "#c63c83",
fill = "#d26ea5",
stroke = 0.8
) +
geom_point(
aes(y = Trait, x = h2.uncorrected),
shape = 21,
size = 1.5,
fill = "white",
colour = "#5e656d",
stroke = 0.8
) +
geom_point(
aes(
y = Trait,
x = h2.region_corrected
),
shape = 21,
size = 1.5,
colour = "#c63c83",
fill = "#d26ea5",
alpha = 0.2
) +
labs(
title = "SNP-based heritability (eta<sup>2</sup> and se) of 56 different traits, uncorrected and <span style='color: #c63c83'>corrected for region</span>",
caption = "Dataviz: Cara R Thompson | Source: https://www.nature.com/articles/s41588-022-01158-0"
) +
scale_colour_identity() +
scale_x_continuous(limits = c(0, 0.45), expand = c(0, 0)) +
theme_minimal() +
theme(
text = element_text(family = "Atkinson Hyperlegible", colour = "#272b2f"),
axis.title = element_blank(),
plot.title = ggtext::element_markdown(
face = "bold",
size = rel(1.5),
hjust = 0.5,
margin = margin(0, 0, 11, 0)
),
legend.position = "none",
strip.text.y.right = element_text(
angle = 0,
colour = "#272b2f",
face = "bold"
),
axis.text = element_text(colour = "#272b2f"),
axis.text.y = element_text(vjust = 0.35),
plot.caption = element_text(hjust = 1, margin = margin(11, 0, 0, 0)),
strip.background = element_rect(colour = "#EAF0F4", fill = "#EAF0F4"),
panel.background = element_rect(colour = "#EAF0F4", fill = "#EAF0F4"),
panel.grid = element_line(colour = alpha("#a2c3bc", 0.3)),
plot.background = element_rect(fill = "#fdfbfb", colour = "#fdfbfb"),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
plot.caption.position = "plot",
plot.title.position = "plot",
plot.margin = margin(rep(22, 2), 11, 22)
) +
facet_grid(rows = "Category", scales = "free_y", space = "free")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment