I have the dataset below:
ww_long<-structure(list(Group = c("OVX", "OVX", "OVX", "OVX", "OVX", "OVX",
"OVX", "OVX", "OVX", "OVX", "OVX", "OVX", "OVX", "SHAM", "SHAM",
"SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM",
"SHAM", "SHAM", "SHAM", "Cycling", "Cycling", "Cycling", "Cycling",
"Cycling", "Cycling", "Cycling", "Cycling", "Cycling", "Cycling",
"Cycling", "Cycling", "Cycling"), Weeks = c(9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21), Weights = c(19.6166666666667, 21.5916666666667, 22.2666666666667,
22.25, 22.4, 23.2, 23.6166666666667, 24.6166666666667, 26.3583333333333,
27.025, 27.9833333333333, 28.7916666666667, 28.975, 18.9666666666667,
19.2916666666667, 19.3666666666667, 19.2166666666667, 19.8583333333333,
19.8083333333333, 20.525, 21.3416666666667, 21.5333333333333,
21.5583333333333, 21.8666666666667, 22.0916666666667, 22.0416666666667,
19.4225, 19.7025, 19.7225, 19.5775, 20.1275, 20.1325, 20.29,
21.065, 21.5325, 21.4375, 21.63, 21.6175, 21.875)), row.names = c(NA,
-39L), class = c("tbl_df", "tbl", "data.frame"))
and with the code below I create this plot but my problem is that for every week my error bars seem to be the same while I different error bar for every week. They all have the same lenth.
ggplot(ww_long, aes(x = as.numeric(Weeks), y = Weights, group = Group, color = Group)) +
geom_line(linewidth = 1.5) +
geom_point(size = 5) +
geom_errorbar(aes(ymin = Weights - sd(Weights), ymax = Weights + sd(Weights)), width = 0.2, size = 1) +
labs(title = "Weekly Weights", x = "Age (Weeks)", y = "Weights (g)", color = "Group") +
scale_x_continuous(breaks = as.numeric(names(ww)[-1]), expand = c(0, 0)) +
scale_y_continuous(breaks = seq(16, 32, by = 2), expand = c(0, 0), labels = function(x) round(x)) +
coord_cartesian(ylim = c(16, 32))
I create
>Solution :
The issue is that all of the Weights data is being used in the sd calculation which makes the error bars the same. You could break your data into groups, calculate the sd, then combine them to make your plot.
library(tidyverse)
ww_long<-structure(list(Group = c("OVX", "OVX", "OVX", "OVX", "OVX", "OVX",
"OVX", "OVX", "OVX", "OVX", "OVX", "OVX", "OVX", "SHAM", "SHAM",
"SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM", "SHAM",
"SHAM", "SHAM", "SHAM", "Cycling", "Cycling", "Cycling", "Cycling",
"Cycling", "Cycling", "Cycling", "Cycling", "Cycling", "Cycling",
"Cycling", "Cycling", "Cycling"), Weeks = c(9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21), Weights = c(19.6166666666667, 21.5916666666667, 22.2666666666667,
22.25, 22.4, 23.2, 23.6166666666667, 24.6166666666667, 26.3583333333333,
27.025, 27.9833333333333, 28.7916666666667, 28.975, 18.9666666666667,
19.2916666666667, 19.3666666666667, 19.2166666666667, 19.8583333333333,
19.8083333333333, 20.525, 21.3416666666667, 21.5333333333333,
21.5583333333333, 21.8666666666667, 22.0916666666667, 22.0416666666667,
19.4225, 19.7025, 19.7225, 19.5775, 20.1275, 20.1325, 20.29,
21.065, 21.5325, 21.4375, 21.63, 21.6175, 21.875)), row.names = c(NA,
-39L), class = c("tbl_df", "tbl", "data.frame"))
ovx <- ww_long %>%
filter(Group == 'OVX') %>%
mutate(upper = Weights + sd(Weights),
lower = Weights - sd(Weights))
sham <- ww_long %>%
filter(Group == 'SHAM') %>%
mutate(upper = Weights + sd(Weights),
lower = Weights - sd(Weights))
cyc <- ww_long %>%
filter(Group == 'Cycling') %>%
mutate(upper = Weights + sd(Weights),
lower = Weights - sd(Weights))
comb <- rbind(ovx, sham)
comb <- rbind(comb, cyc)
comb %>%
ggplot(aes(x = as.numeric(Weeks), y = Weights, group = Group, color = Group)) +
geom_line(linewidth = 1.5) +
geom_point(size = 5) +
geom_errorbar(aes(ymin = lower, ymax = upper, width = 0.2), linewidth = 1) +
labs(title = "Weekly Weights", x = "Age (Weeks)", y = "Weights (g)", color = "Group") +
scale_x_continuous(breaks = seq(9,21,1), expand = c(0, 0)) +
scale_y_continuous(breaks = seq(16, 32, by = 2), expand = c(0, 0), labels = function(x) round(x)) +
coord_cartesian(ylim = c(16, 32))

Created on 2023-11-30 with reprex v2.0.2
