A quick ggplot2 hack (multiple dataframes)

[This article was first published on Timothée Poisot » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’m starting to get familiar with ggplot2, and I really like it. I just found a very quick way to use several dataframes within the same plot, provided that the dataframes share columns names.

One obvious application is the production of graphs with the mean (obtained by aggregate) superposed to the original raw data.

You can do this in ggplot2 simply with something along the lines of

dat <- read.table(‘sampling.txt’)
dat.me <- aggregate(dat,list(NEST=dat$NEST,NRR=dat$NRR,MES=dat$MES),mean)
base <- ggplot(dat,aes(x=NRR,y=PROP,colour=MES))
base+geom_point() + geom_line(data=dat.me)

By changing the data argument, you will recycle the aes settings. This is really handy.

The result is

ggplot-sevdf.png

Now, of course, is someone knows a simpler way to do it, I’d like to know!

To leave a comment for the author, please follow the link and comment on their blog: Timothée Poisot » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)