ggplot: a plotting alternative to R base, and lattice
May 14th, 2007If you found the tutorial on drawing graphs using R a bit of a kerfuffle, there’s a good introduction on drawing graphs using the ggplot package. An alternative to the R base and lattice packages - so now you’ve got three to choose from.
December 8th, 2008 at 6:12 pm
I can highly recommend ggplot. The site you link to is for the older ggplot (version 1), while the latest version is ggplot2. The syntax has been dramatically changed, for the better. The ggplot2 website is at: http://had.co.nz/ggplot2/, though unfortunately it does not seem to have an updated waiter tips example.
I’m no expert, but I can give it a shot. The original plot would be the same, though you have to explicitly plot p, with:
> p
After that, it all changes. To add facets, you would do something like:
> p + facet_grid (sex ~ .)
which is a simpler way to add/modify plots, and scales better as you add more things like geom’s, etc. On the other hand, the multiple ablines is more complex:
> p + geom_abline (aes(slope=b, intercept=a), data=data.frame(a=c(0, 0, 0), b=c(0.1, 0.15, 0.2)))
For the smoother:
> p + geom_smooth (method = “lm”)
which draws the smoothing line with confidence intervals. If no method= is specified, it defaults to a loess smoother.
You can build graphs piece-by-piece with “+”, which provides total flexibility, but you can also do many things with the all-in-one qplot. For example, the smooth example becomes:
> qplot (total_bill, tip, data = tips, geom=c(”smooth”, “point”))
which, by the way, plots the smooth first and the point’s second so that the points overlay the smoother.