Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Created October 25, 2024 19:01
Show Gist options
  • Save benmarwick/9778b094e95ee6f06d2db6e1831cc225 to your computer and use it in GitHub Desktop.
Save benmarwick/9778b094e95ee6f06d2db6e1831cc225 to your computer and use it in GitHub Desktop.
Take output from rcarbon::calibrate and plot with ggridges::geom_ridgeline
# Load required libraries
library(rcarbon)
library(ggplot2)
library(ggridges)
library(dplyr)
# Step 1: Calibrate dates
# Example data: replace with your actual radiocarbon dates and errors
dates <- c(4500, 4800, 5000) # Replace with your dates
errors <- c(30, 50, 100) # Replace with your errors
calibrated_dates <- calibrate(dates, errors)
# Step 2: Prepare data for plotting
# Extract the grids for each calibrated sample and reshape for plotting
cal_data_long <- bind_rows(lapply(seq_along(calibrated_dates$grids), function(i) {
# Extract each grid's calBP and PrDens, and add a sample identifier
grid_data <- calibrated_dates$grids[[i]]
data.frame(
Sample = paste("Sample", i),
Year = grid_data$calBP,
Probability = grid_data$PrDens
)
}))
# Step 3: Plot using ggridges::geom_ridgeline
ggplot(cal_data_long,
aes(x = Year,
y = Sample,
height = Probability,
group = Sample)) +
geom_ridgeline(fill = "lightblue",
color = "darkblue",
scale = 100) +
scale_x_reverse() + # Reverse x-axis for cal BP to AD
labs(
x = "Calibrated Years BP",
y = "Sample",
title = "Calibrated Radiocarbon Date Distributions"
) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment