I am creating multiple plots using a loop but encountering an issue where the plots appear to be the same as each other even though the underlying data values are different.
For example, when the number is 3, I should obtain three different plots, however, all plots are the same as each other.
all_plot_kmi <- NULL
for (i in 1:number) {
plot4_k1 <- ggplot(data = final_result, aes(t.est, final_result[, 17+6*(i-1)])) +
geom_line(color = "red", size = 0.75) +
geom_line(aes(t.est, final_result[, 18+6*(i-1)]), size = 0.8, color = "blue", linetype = "dashed") +
geom_line(aes(t.est, final_result[, 19+6*(i-1)]), size = 0.8, color = "blue", linetype = "dashed") +
labs(
title = "Plot of the time-varying covariate effect on outcome",
x = "Time Sequence",
y = paste("kmi_", i, "_coefficient", sep = "")) +
scale_x_continuous(breaks = seq(l, u, NI))
all_plot_kmi <- c(all_plot_kmi, list(plot4_k1))}
I don't think the problem lies in the way I am specifying the aesthetics (aes) inside the ggplot function. Because it doesn't work after I modify the aes calls inside the ggplot function to correctly reference the variable names based on the loop index i.
And the data file is not blank or null.