Plotting xyplot with regression line on lattice graphics

前端 未结 1 1771
既然无缘
既然无缘 2020-12-31 07:58

I have loaded the lattice package. Then I run:

> xyplot(yyy ~ xxx | zzz, panel = function(x,y) { panel.lmline(x,y)}

This produces panel

相关标签:
1条回答
  • 2020-12-31 08:20

    All you really need is:

    xyplot(yyy ~ xxx | zzz, type = c("p","r"))
    

    where the type argument is documented in ?panel.xyplot

    I won't quote it all but

    type: character vector consisting of one or more of the following:
          ‘"p"’, ‘"l"’, ‘"h"’, ‘"b"’, ‘"o"’, ‘"s"’, ‘"S"’, ‘"r"’,
          ‘"a"’, ‘"g"’, ‘"smooth"’, and ‘"spline"’.  If ‘type’ has more
          than one element, an attempt is made to combine the effect of
          each of the components.
    
          The behaviour if any of the first six are included in ‘type’
          is similar to the effect of ‘type’ in ‘plot’ (type ‘"b"’ is
          actually the same as ‘"o"’).  ‘"r"’ adds a linear regression
          line (same as ‘panel.lmline’, except for default graphical
          parameters). ‘"smooth"’ adds a loess fit (same as
          ‘panel.loess’).  ‘"spline"’ adds a cubic smoothing spline fit
          (same as ‘panel.spline’).  ‘"g"’ adds a reference grid using
          ‘panel.grid’ in the background (but using the ‘grid’ argument
          is now the preferred way to do so).  ‘"a"’ has the effect of
          calling ‘panel.average’, which can be useful for creating
          interaction plots.  The effect of several of these
          specifications depend on the value of ‘horizontal’.
    

    You can, as I showed above, add these in series by passing type a character vector. Essentially, your code gave the same result as type = "r", i.e. only the regression line was drawn.

    The panel argument of xyplot, and Lattice plotting functions in general, is extremely powerful but not always required to so quite complex things. Basically you need to pass panel a function which will draw things on each panel of the plot. To modify your code to do what you wanted, we need to add a call to panel.xyplot() as well. E.g.:

    xyplot(yyy ~ xxx | zzz,
           panel = function(x, y, ...) {
                     panel.xyplot(x, y, ...)
                     panel.lmline(x, y, ...)
                   })
    

    It is also very useful to pass all other arguments on the individual panel functions via ..., in which case you need ... as an argument in your anonymous functions (as shown above). In fact, you can probably write that panel function part as:

    xyplot(yyy ~ xxx | zzz,
           panel = function(...) {
                     panel.xyplot(...)
                     panel.lmline(...)
                   })
    

    but I usually add the x and y arguments just to be clear.

    0 讨论(0)
提交回复
热议问题