you're reading...
Note to self, Tutorial

ggplot2 in loops and multiple plots

# ggplot2 version
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])) + 
  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)

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.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


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.