//
archives

loop

This tag is associated with 1 post

ggplot2 in loops and multiple plots

# ggplot2 version
par(ask=TRUE)
out <- NULL
p <- ggplot(data.frame(movieSummary), aes(y=movieSummary$Profit)) + ylab("Profits") + scale_y_continuous(labels=comma)
for(i in 2:(ncol(movieSummary)-1)) {
  p <- p + aes_string(x = names(movieSummary)[i]) + xlab(colnames(movieSummary[i])) + 
    geom_point(shape=1) 
  print(p)
  out[[i]] <- p
  # ggsave(filename=paste("Plot of Profit versus",colnames(movieSummary[i]),".pdf",sep=" "), plot=p)
}
 
grid.arrange(out[[2]], out[[3]], out[[4]], out[[5]], out[[6]], out[[7]],
             out[[8]], nrow = 7)
dev.off()

Created by Pretty R at inside-R.org

Too tired to explain after so much debugging. But the key is at the line here

p + aes_string(x = names(mydata)[i])

Use aes_string instead of aes, so that when you look at summary(ggplot_obj), the mapping for x-values that are changing will be the actual string and not a variable i.

Advertisements
mathbabe

Exploring and venting about quantitative issues

The Stone and the Shell

Using large digital libraries to advance literary history

Hi. I'm Hilary Mason.

Zoom out, zoom in, zoom out.

Introduction to Data Science, Columbia University

Blog to document and reflect on Columbia Data Science Class

statMethods blog

A Quick-R Companion

the Tarzan

[R] + applied economics.

4D Pie Charts

Scientific computing, data viz and general geekery, with examples in R and MATLAB.