Created
March 12, 2022 18:11
-
-
Save DrNickRedfern/9d474a150003faf2e6c5755dd98ef316 to your computer and use it in GitHub Desktop.
See the repository for this function: https://github.com/DrNickRedfern/loessggplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loessggplot <- function(x, low = 0.1, high = 0.9, step = 0.01, title = "", ticks = 0.1){ | |
n <- length(x) | |
t <- cumsum(x); t <- 100*t/max(t) # normalize the running time to percent | |
# Create an empty data fame to store the results of the for loop | |
df <- data.frame() | |
# Loop over the sequence of spans for the loess smoothers and store the result | |
for(s in seq(low, high, step)) { | |
fit <- loess(x ~ t, span = s, degree = 2)$fitted | |
sn <- as.numeric(rep(s, n)) | |
df_a <- data.frame(cbind(sn, t, fit)) | |
df <- rbind(df,df_a) | |
} | |
# Plot the data | |
ggplot() + | |
geom_vline(xintercept = seq(25, 75, 25), colour="black", linetype = "longdash") + # reference lines | |
geom_line(data = df, aes(x = t, y = fit, group = sn, colour = sn)) + # loess smoothers | |
scale_x_continuous(name = "\nRunning time (%)", limits = c(0, 100), | |
breaks = seq(0, 100, 25), labels = seq(0, 100, 25)) + | |
labs(title = title, y = "Fitted values (s)\n") + | |
scale_colour_viridis_c(name = "Span", breaks = seq(low, high, ticks)) + | |
guides(colour = guide_colourbar(barwidth = 20, barheight = 1, title.position = "top")) + | |
theme_classic() + | |
theme(legend.position = "bottom", | |
plot.title = element_text(face = "bold")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment