在晶格图形上用回归线绘制 xyplot

Posted

技术标签:

【中文标题】在晶格图形上用回归线绘制 xyplot【英文标题】:Plotting xyplot with regression line on lattice graphics 【发布时间】:2012-10-09 22:30:07 【问题描述】:

我已经加载了 lattice 包。 然后我运行:

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

这会产生图面板,显示回归线,没有 xyplots。 我正在做 panel.lmline 没有完全理解它是如何完成的。我知道有一个数据参数,数据是什么,知道我有3个变量xxxyyyzzz

【问题讨论】:

【参考方案1】:

你真正需要的是:

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

type 参数记录在 ?panel.xyplot

我不会全部引用,而是

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’.

正如我在上面展示的那样,您可以通过传递type 一个字符向量来将它们串联起来。本质上,您的代码给出了与type = "r" 相同的结果,即绘制了回归线。

xyplotpanel 参数和一般的格子绘图功能非常强大,但并不总是需要如此复杂的事情。基本上,您需要传递panel 一个函数,该函数将在绘图的每个面板上绘制内容。要修改您的代码以执行您想要的操作,我们还需要添加对 panel.xyplot() 的调用。例如:

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

通过... 在各个面板函数上传递所有其他参数也非常有用,在这种情况下,您需要... 作为匿名函数中的参数(如上所示)。实际上,您可以将面板功能部分写成:

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

但为了清楚起见,我通常会添加 xy 参数。

【讨论】:

以上是关于在晶格图形上用回归线绘制 xyplot的主要内容,如果未能解决你的问题,请参考以下文章

“用户警告:Matplotlib 当前使用的是 agg,这是一个非 GUI 后端,因此无法显示该图。”在 Pycharm 上用 pyplot 绘制图形时

支持向量回归 (SVR) 在 Ubuntu 18.04 LTS 中不绘制图形

R语言基本功:绘制带边际图的散点图

在 QQuickPaintedItem 上用鼠标绘制

在画布上用鼠标绘制矩形与鼠标坐标不同步

在画布上用鼠标画线:Java awt