0

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!

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Hints: you don't initiate your for-loop correctly, and subfieldsdata$i doesn't exists. – Heroka Dec 16 '15 at 16:47
  • The way to refer to the `i`th-variable is with `object[[i]]`, not with `$i` – IRTFM Dec 16 '15 at 19:54
  • Where is `subcor` coming from? In this line, `corr <- format(subcor, digits=3, nsmall=3)` – Hillary Dec 16 '15 at 20:49

2 Answers2

0

May not be the best way to do it, but please see the code below to be able to run this in a loop. Not the prettiest graph I produced, but easy to make pretty with ggplot2. If you had less variables, I would recommend using the corrplot package or the performanceAnalytics package to plot correlations.

# The method the OP was after
require(ggplot2)
for (x in colnames(mtcars)[-1]){ 
  corr <- cor(mtcars[, "mpg"], mtcars[, x], use="everything", method="pearson")
  corr <- format(corr, digits=3, nsmall=3)

  gp <- ggplot(data=mtcars, aes_string(x= "mpg",y = x)) + geom_point()
  gp <- gp + stat_smooth(method="lm") + labs(x = "mpg", y = x)+
    theme_linedraw(base_size=25) +
    annotate("text", x=15, y=5, label=paste0("Pearson r =", corr))
  print(gp)
}

enter image description here

Raad
  • 2,675
  • 1
  • 13
  • 26
0
### CREATE SOME SAMPLE DATA
subfieldsdata <- data.frame("subadj"=rnorm(100,0,1),"presuadj"=rnorm(100,1,2), "CA1adj"=rnorm(100,2,3), "CA3adj"=rnorm(100,2,4), "CA4adj"=rnorm(100,3,2), "DGadj"=rnorm(100,1,3))
myData <- data.frame('NP_Age'=sample(1:100,100))

### MAKE THE LOOP
for (i in 1:ncol(subfieldsdata)) {

  output <- cor(subfieldsdata[,i],myData$NP_Age,use="everything", method="pearson")
  corr <- format(output, digits=3, nsmall=3) #I ASSUME 'SUBCOR' WAS A TYPO??

    gp <- ggplot(data=myData,aes(x=myData$NP_Age,y=subfieldsdata[,i])) + geom_point() 

    gp <- gp +
    stat_smooth(method="lm") +
    labs(x="age", y=colnames(subfieldsdata[i])) +
    theme_linedraw(base_size=25) +
    annotate("text", x=c(75,90), y=1000, label=c("Pearson r =", corr)) 
    print(gp)
}

EXAMPLE PLOT

Hillary
  • 785
  • 1
  • 6
  • 17