Plotting a grid behind data, not in front in R

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

    Another methods for grid lines in background:

    A)

    hist( y, panel.first=grid() )  # see: help( plot.default )
    box()
    

    B)

    plot.new()                     # new empty plot
    nv <- length( pretty(x) ) - 1  # number of vertical grid lines (or set by hand)
    nh <- length( pretty(y) ) - 1  # number of horizontal grid lines (or set by hand)
    grid( nx = nv, ny = nh )       # preplot grid lines
    par( new = TRUE )              # add next plot
    plot( x, y )                   # plot or hist, etc
    box()                          # if plot hist
    

    Arbitrary lines in background with abline:

    C)

    How do I draw gridlines using abline() that are behind the data?

    D)

    # first, be sure there is no +/-Inf, NA, NaN in x and y
    # then, make the container plot with two invisible points:
    plot( x = range( pretty( x ) ), y = range( pretty( y ) ), type = "n", ann = FALSE )
    abline( h = hlines, v = vlines )  # draw lines. hlines, vlines: vectors of coordinates
    par( new = TRUE )                 # add next plot. It is not necessary with points, lines, segments, ...
    plot( x, y )                      # plot, hist, etc
    box()                             # if plot hist
    

提交回复
热议问题