Plotting a grid behind data, not in front in R

前端 未结 5 868
一生所求
一生所求 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:23

    In R, order matters when you plot. As you've discovered, adding things to a plot adds on top of what you've plotted before. So we need a way to plot the grid first and then the histogram. Try something like this:

    plot(1:10,1:10,type = "n")
    grid(10,10)
    hist(rnorm(100,5,1),add = TRUE)
    

    enter image description here

    I haven't recreated your example, since it isn't reproducible, but this general idea should work. But the key idea is to create an empty plot with the correct dimensions using the type = "n" option to plot, then add the grid, then add the histogram using the add = TRUE argument.

    Note that the add argument is actually for plot.histogram, hist passes it along via ....

提交回复
热议问题