Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting xyplot with regression line on lattice graphics

Tags:

r

lattice

panel

I have loaded the lattice package. Then I run:

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

This produces panels of plot, showing the regression line, without the xyplots. I am doing the panel.lmline without fully understanding how it is done. I know there is a data argument, what is the data, knowing that I have the 3 variables xxx, yyy, zzz?

like image 537
Selvam Avatar asked Oct 19 '12 10:10

Selvam


People also ask

How do you add a regression line to a plot?

A regression line will be added on the plot using the function abline(), which takes the output of lm() as an argument. You can also add a smoothing line using the function loess().

What is XYPlot?

The XYPlot resource allows you to plot data onto the X and Y axis of the graph. You can choose to plot any number of parameters as a function of a single independent variable. GMAT allows you to plot user-defined variables, array elements, or spacecraft parameters.

Which of the following command is used for producing a scatter plot from Lattice library?

The explanation is: plot() produces a scatterplot.

What library is XYPlot in R?

The lattice library offers the xyplot() function. It builds a scatterplot for each levels of a factor automatically.


1 Answers

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.

like image 166
Gavin Simpson Avatar answered Oct 02 '22 19:10

Gavin Simpson