Plotting a grid behind data, not in front in R

前端 未结 5 885
一生所求
一生所求 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 00:28

    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.

提交回复
热议问题