Skip to content

Instantly share code, notes, and snippets.

@tyleransom
Created February 21, 2023 04:12
Show Gist options
  • Save tyleransom/1abd1d454132d088f401313f3a5827a0 to your computer and use it in GitHub Desktop.
Save tyleransom/1abd1d454132d088f401313f3a5827a0 to your computer and use it in GitHub Desktop.
graph-spline
library(ggplot2)
# create a matrix with the given points
pts <- matrix(c(3, 3, 3, 3, 3, 3, 3, 0.5, -1, -3, 1, 1.5, 2.75, 3, 1.25, 1.75, 0, 4, 8, 12, 16, 20, 23, 24, 25, 37, 38.5, 48, 60, 69, 71, 84), ncol = 2, byrow = F)
# sort the points by x value
pts <- pts[order(pts[, 2]), ]
# create the spline interpolation function
interp <- splinefun(pts[, 2], pts[, 1], method = "natural")
# evaluate the function at a range of x values
x <- seq(0, 90, length.out = 1000)
y <- interp(x)
# plot the function
ggplot(data.frame(x, y), aes(x, y)) +
geom_line() +
labs(x = "Hours", y = "Subjective Well-Being\n") +
theme_bw() +
scale_y_continuous(breaks = NULL) +
theme(panel.grid = element_blank(),
axis.line.y = element_line(),
axis.line.x = element_line(),
panel.border = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment