When I'm generating graphics for publications and talks, I tend to use ggplot2
. However, for very large data sets where I want to generate a quick plot or for courses where students don't have a good grounding in R, I use the base graphics.
Are there any nice (simple!) ways of spicing up R graphics? For example, do you use a nice combination of colours and line types. I tend to do something like:
#Functional but not that nice
plot(x1,y1, type="l")
lines(x2, y2, col=2, lty=2)
In particular, I'm thinking about the plot
, hist
, and density
functions, but I suppose this question applies to all base R graphics.
In R, graphs are typically created interactively. Creating a new graph by issuing a plotting command, such as plot() , hist() , boxplot() , among others, will typically overwrite a previous graph. In addition one can specify fonts, colors, line styles, axes, reference lines, etc.
R language supports a rich set of packages and functionalities to create the graphs using the input data set for data analytics. The most commonly used graphs in the R language are scattered plots, box plots, line graphs, pie charts, histograms, and bar charts.
R includes at least three graphical systems, the standard graphics package, the lattice package for Trellis graphs and the grammar-of-graphics ggplot2 package.
Learn to use par
. At the very least, make the y-axis label horizontal with par(las = 1)
. Manually adjusting margins with the mar
and oma
settings of par
are also useful.
Use hue-chroma-luminance (HCL) colours, via the vcd
package, especially for plots involving area (histograms or whatever).
The first half of Paul Murrell's R Graphics gives you advice on customising base graphics. If you want more general advice on drawing good graphs, Stephen Few's Now You See It is my personal favourite, and Edward Tufte's books are all staples.
As an alternative to teaching base
graphics, you could use latticist
to make lattice
easier to learn.
I often skip position 3 in the default palette (green) because it generates dichromat-unfriendly plots. I should probably use palette(palette()[c(1:2,4:8,3)])
to do this automatically, but I tend to just do it by hand. I recommend the RColorBrewer
package too.
Andrew Gelman (a pretty well-known statistician at Columbia with a very entertaining blog) would like you to adjust the margin and tick spacing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With