I don't have a solution for my problem. I want to present three different median lines from a dataset which works without a problem. Now I would like to add a ribbon for the IQR or errorbars which would require calculation of upper and lower borders. This does not work.
Those are my variables
otherside_surgery_yesno - three groups here Jahre - those are the years I want to show on the x-axis po_constant_impaired_score_a - this is the value I would like to show on y-axis
The code below works nicely but I can't find any simple solution adding IQR as ribbon or errorbars.
both_sides %>%
ggplot() +
# RTSA
stat_summary(data = both_sides %>% filter(is.na(otherside_surgery_yesno)),
aes(x = Jahre,
y = po_constant_impaired_score_a),
fun = median,
geom = "line",
color = "red") +
# Healthy without surgery
stat_summary(data = both_sides %>% filter(otherside_surgery_yesno == "0"),
aes(x = Jahre,
y = po_constant_impaired_score_a),
fun = median,
geom = "line",
color = "darkgreen") +
stat_summary(data = both_sides %>% filter(!is.na(otherside_surgery_yesno)),
aes(x = Jahre,
y = po_constant_impaired_score_a),
fun = median,
geom = "line",
color = "blue") +
theme_classic() +
ylab("Constant Score absolute") +
xlab("Time (years)")
calculate_lower <- function(data) {
return(quantile(data, 0.25))
}
calculate_upper <- function(data) {
return(quantile(data, 0.75))
}
geom_errorbar(
aes(ymin = calculate_lower(data), ymax = calculate_upper(data)),
width = 0.2
)