I am trying to visualize my longtudinal data by graphs using ggplot through for loop.
for(i in colnames(dat_longC)[c(3,5:10,14,17:19,30:39)]){
print(ggplot(dat_longC, aes(x = exam, y = i, group = zz_nr))+ geom_point()+
geom_line() + xlab("Examination") + ylab(i))}
}
when I use the ggplot command in a for loop, I only get a line extending between examination times. If I use the same command on a single variable, it works and gives me trajectory graphs. What do you think could be the problem?
Your problem is that you are using
ito indicate the column. That is just an index, so it does not know what you are actually trying to plot. you really wantcolnames(dat_longC)[i]. Unfortunately, that will still not work because you are using a string as a variable name, which does not work forggplot2. Instead, you will likely need!!sym(colnames(dat_longC)[i]). I can't really test without your data, but here is some example code to help guide you.