2

I am calculating residuals from many regressions to plot these residuals afterwards with ggplot.

I took the 6 variables of interest from my datafile and created the subfieldsdata file (6 columns, 116 rows).

The linear regression models:

buildres <- sapply(seq_along(subfieldsdata), function(i) lm(subfieldsdata[,i] ~ myData$NP_Age+myData$sex+myData$YrsOfEd), simplify=FALSE, USE.NAMES=TRUE)

Calculation of the residuals:

getres <- vapply(buildres, stdres, numeric(116))

Adding column names:

colnames(getres) <- c("Subiculum residuals", "Presubiculum residuals", "CA1 residuals", "CA3 residuals", "CA4 residuals", "Dentate gyrus residuals")

Then I have a for loop for ggplot:

for (i in 1:ncol (getres)) {
gp <- ggplot(data=subfieldsdata,aes(x=ECtau.res,y=getres[,i])) + geom_point(colour="Blue", shape=17, size=5)
gp <- gp +
stat_smooth(method="lm", colour="Blue", size=2, fill="Blue") +
scale_y_continuous(breaks=seq(-4, max(getres[,i])*1.1, 0.5)) +
theme_grey(base_size=35) +
labs(x="EC tau residuals", y=colnames(getres)[i]))
 print (gp)
ggsave(sprintf("ECtau%s.png", colnames((getres)[i]),gp))
}

I get this error message:

Saving 7 x 7 in image
Error in strsplit(filename, "\\.")[[1]] : subscript out of bounds

I cannot be the dimensions:

dim(getres)
[1] 116   6

I also checked the colnames:

> colnames(getres)
[1] "Subiculum residuals"     "Presubiculum residuals"  "CA1 residuals"           "CA3 residuals"          
[5] "CA4 residuals"           "Dentate gyrus residuals"

Do you know what might be the problem?

Thanks!

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • It's difficult to guess what may be the error without a reproducible example but I'm guessing that the output from *sprintf* is the problem. Just to see what's going on, did you try creating the desired file name outside the *ggsave*? – Konrad Dec 20 '15 at 23:46
  • Is there something wrong with my answer Heidi? – Mike Wise Dec 21 '15 at 15:13

1 Answers1

2

Welcome to Stack Overflow Heidi :)

This (your plot with some fake data and a temp variable for the file name) works fine for me:

# Weird Error Message

ncol <- 6
nrow <- 116
subfieldsdata <- data.frame(matrix(rnorm(ncol*nrow),nrow,ncol))
colnames(subfieldsdata)[1] <- "ECtau.res"

getres <- data.frame(matrix(rnorm(ncol*nrow),nrow,ncol))
colnames(getres) <- c("Subiculum residuals", "Presubiculum residuals",
                      "CA1 residuals", "CA3 residuals", 
                      "CA4 residuals", "Dentate gyrus residuals")

for (i in 1:ncol (getres)) {
  gp <- ggplot(data=subfieldsdata,aes(x=ECtau.res,y=getres[,i])) + 
        geom_point(colour="Blue", shape=17, size=5)
  gp <- gp +
    stat_smooth(method="lm", colour="Blue", size=2, fill="Blue") +
    scale_y_continuous(breaks=seq(-4, max(getres[,i])*1.1, 0.5)) +
    theme_grey(base_size=35) +
    labs(x="EC tau residuals", y=colnames(getres)[i])
  print (gp)
  fname <- sprintf("ECtau%s.png", colnames((getres)[i]))
  print(sprintf("Saving %s ",fname))
  ggsave(fname,gp)
}

Output:

[1] "Saving ECtauSubiculum residuals.png "
Saving 7.62 x 7.11 in image
[1] "Saving ECtauPresubiculum residuals.png "
Saving 7.62 x 7.11 in image
[1] "Saving ECtauCA1 residuals.png "
Saving 7.62 x 7.11 in image
[1] "Saving ECtauCA3 residuals.png "
Saving 7.62 x 7.11 in image
[1] "Saving ECtauCA4 residuals.png "
Saving 7.62 x 7.11 in image
[1] "Saving ECtauDentate gyrus residuals.png "
Saving 7.62 x 7.11 in image
>

The last plot:

enter image description here

And their was a typo in your code (one too many parenthesis on the labs argument), so I am thinking that this is maybe not an exact duplicate of your problem code. Clearly the problem is in your filename, something is going wrong with that filename, which is why I printed it out.

I would suggest printing out the filename and seeing what went wrong.

Now that I think of it, another guess is that you have some periods (that is "." characters) in the column names of your subfieldsdata dataframe that are confusing the ggsave routine. This is probably the problem - try using dashes or something instead.

Also take this as a lesson in how to create a properly reproducible question. :)

Mike Wise
  • 22,131
  • 8
  • 81
  • 104