I am still learning R, but going well. I want to create a loop in which I correlate 6 variables against 1 other variable and then make a plot of them with ggplot. I can do this for 1 variable separately, but I would like to make a loop of it. The data file contains more than 300 variables, so I have first created a separate data frame for the 6 variables:
subfieldsdata <-myData[,c("subadj","presuadj", "CA1adj", "CA3adj", "CA4adj", "DGadj")]
And then tried to run this loop
for (i in 1:6 (subfieldsdata))
{output <- cor(subfieldsdata$i,myData$NP_Age,use="everything", method="pearson")
corr <- format(subcor, digits=3, nsmall=3)
gp <- ggplot(data=myData,aes(x=myData$NP_Age,y=subfieldsdat$i))
gp+geom_point()+stat_smooth(method="lm")+labs(x="age", y="i")+theme_linedraw(base_size=25)+annotate("text", x=c(88,90), y=1000, label=c("Pearson r =", corr))}
This is not working and I am now stuck... How can I solve this loop? Is there also a way to also name the plots differently for each variable of the loop?
Thanks!