Prettier base plots in R

R’s base graphics system is notable for the minimal design of its plots. Basic usage is very simple, although more complex customization capabilities are not user friendly. Hence I wrapped the plot and hist functions to improve their default behavior.

Any argument usually passed to plot or hist can also be passed to the two wrapper functions pretty_plot and pretty_hist. A comparison is shown below; “prettified” functions are on the right (obviously!).

par(mfcol=c(1,2))
plot(cars); pretty_plot(cars)

cars.png
data = rnorm(100)
hist(data, xlab="", ylab="", main=""); pretty_hist(data)

100
data = rnorm(100000)
hist(data, xlab="", ylab="", main=""); pretty_hist(data)

100000

Source

WordPress.com has problems with code integration. This is the best I could do.

pretty_plot <- function(x, y, xlab = NULL, ylab = NULL, col = rgb(0,.3,.8, alpha=.6), pch = 20, bty="n",lwd = .75, ...) {
 #
 # Wrapper to R's plot function. Overrides default behavior for better plots.
 #
 # Examples:
 # > pretty_plot(cars)
 #
 # cc-by O.B. 2018

 ## Figuring out the labels. In simple cases automatic labelling is used. Otherwise labels are empty by default.
 if (missing(y)) {
   if (is.null(xlab) && is.null(ylab)) {
     plot(x, col=col, pch=pch, bty=bty, lwd=lwd, tcl=-0.15, mgp=c(2,0.25,0), cex.axis=.8, ...);
   }
   else {
     xlab <- if(is.null(xlab)) "" else xlab
     ylab <- if(is.null(ylab)) "" else ylab
     plot(x, xlab=xlab, ylab=ylab, col=col, pch=pch, bty=bty, lwd=lwd, tcl=-0.15, mgp=c(2,0.25,0), cex.axis=.8, ...);
   }
 }
 else {
   xlab <- if (missing(xlab)) deparse(substitute(x)) else xlab;
   ylab <- if (missing(ylab)) deparse(substitute(y)) else ylab;
   plot(x, y, xlab=xlab, ylab=ylab, col=col, pch=pch, bty=bty, lwd=lwd, tcl=-0.15, mgp=c(2,0.25,0), cex.axis=.8, ...);
 }
}


pretty_hist <- function(x, xlab = NULL, ylab = NULL, main = NULL, col = rgb(0,.3,.8,.5), border = rgb(0,0,0,0), freq = F, breaks = NULL, yaxt = "n", lwd = .75, ...) {
 #
 # Wrapper to R's _hist_ function. Overrides default behavior for better plots.
 #
 # Examples
 # > pretty_hist(rnorm(100))
 # > pretty_hist(rnorm(100000))
 #
 # cc-by O.B. 2018


 ## Automatic number of bins.
 if (is.null(breaks)) {
   breaks <- 3*floor(length(x)^.33) + 3
 }
 
 h = hist(x, xlab=xlab, ylab=ylab, main=main, col=col, border=border, freq=freq, breaks=breaks, yaxt=yaxt, lwd=lwd, tcl=-0.15, mgp=c(2,0.25,0), cex.axis=.8, ...)

 ## Bins separation lines. The separation disapears at high bin count.
 nBreak = length(h$breaks)
 if (nBreak < 150) {
   abline(v = h$breaks, col = rgb(1,1,1,2*exp(-nBreak/50)/(1+exp(-nBreak/50))), lwd=0.5)
 }
}

 

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s