Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improvements to the base R graphics

Tags:

r

graphics

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.

like image 551
csgillespie Avatar asked Jul 05 '10 09:07

csgillespie


People also ask

What is R base 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.

Is R good for making graphs?

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.

How many types of graphics are supported by R?

R includes at least three graphical systems, the standard graphics package, the lattice package for Trellis graphs and the grammar-of-graphics ggplot2 package.


2 Answers

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.

like image 122
Richie Cotton Avatar answered Oct 19 '22 23:10

Richie Cotton


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.

like image 2
Ben Bolker Avatar answered Oct 20 '22 00:10

Ben Bolker