Plotting a grid behind data, not in front in R

前端 未结 5 869
一生所求
一生所求 2020-12-30 23:45

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.

    his         


        
5条回答
  •  梦毁少年i
    2020-12-31 00:20

    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.

提交回复
热议问题