A quick ggplot2 hack (multiple dataframes)

September 9, 2010
By

(This article was first published on Timothée Poisot » R, and kindly contributed to R-bloggers)

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 his blog: Timothée Poisot » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.