Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a grid behind data, not in front in R

Tags:

plot

r

I like to produce my own grid lines when plotting so I can control tick marks, etc. and I am struggling with this with the 'hist' plotting routine.

    hist(WindSpeed, breaks=c(0:31), freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, tck=1, font.lab=2)     axis(1, tck=1, ,col.ticks="light gray")     axis(1, tck=-0.015, col.ticks="black")     axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")     axis(2, tck=-0.015)     minor.tick(nx=5, ny=2, tick.ratio=0.5)     box() 

Plot: enter image description here

I have then just been able to use the 'lines' or 'points' command to replot the data over top for other types of plots, but with the histogram its not so easy.

Any help would be great.

I added my code below and image based upon John's response...

I added my code below and image based upon John's response...

hist(WindSpeed, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2) axis(1, tck=1, col.ticks="light gray") axis(1, tck=-0.015, col.ticks="black") axis(2, tck=1, col.ticks="light gray", lwd.ticks="1") axis(2, tck=-0.015) minor.tick(nx=5, ny=2, tick.ratio=0.5) box() hist(WindSpeed, add=TRUE, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)", main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2) 

enter image description here

like image 978
RWJ Avatar asked Dec 09 '11 21:12

RWJ


2 Answers

Actually, R has a way to do this! It's the panel.first argument to plot.default, which hist calls to do most of the work. It takes an expression which is evaluated "after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths," to quote from ?plot.default.

hist(WindSpeed, breaks=c(0:31), freq=TRUE, col="blue", xaxt="n", yaxt="n",       xlab="Wind Speed (m/s)", main="Foo",       cex.main=1.5, cex.axis=1, cex.lab=1, tck=1, font.lab=2,      panel.first={         axis(1, tck=1, col.ticks="light gray")         axis(1, tck=-0.015, col.ticks="black")         axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")         axis(2, tck=-0.015)         minor.tick(nx=5, ny=2, tick.ratio=0.5)         box()  }) 

See How do I draw gridlines using abline() that are behind the data? for another question that uses this method.

like image 165
Aaron left Stack Overflow Avatar answered Oct 03 '22 14:10

Aaron left Stack Overflow


This is relatively easy.

Generate the histogram but don't plot it.

h <- hist(y, plot = FALSE) 

Now generate your base plot... I've added some features to make it look more like a standard historgram

plot(h$mids, h$counts, ylim = c(0, max(h$counts)), xlim = range(h$mids)*1.1,      type = 'n', bty = 'n', xlab = 'y', ylab = 'Counts', main = 'Histogram of y') 

add your grid

grid() 

add your histogram

hist(y, add = TRUE) 

Or, as I discovered through this process... you can do it even easier

hist(y) grid() hist(y, add = TRUE, col = 'white') 

This last method is just redrawing the histogram over the grid.

like image 45
John Avatar answered Oct 03 '22 13:10

John