首页 > 解决方案 > 将相同的密度曲线添加到 R 中的绘图

问题描述

我想知道如何将相同的密度曲线添加到 R 中的xyplot()fromlattice包中(请参阅下面的可重现代码)?

library(lattice)

xyplot((1:32*.01)~wt|gear , data = mtcars)

lines(density(rnorm(1e3, 3.5))) # add this to all plot panes in `xyplot` above

标签: rplotlattice

解决方案


我们可以使用layer

library(lattice)
library(latticeExtra)
foo <- xyplot((1:32*.01)~wt|gear , data = mtcars)
foo + 
      layer(panel.densityplot(rnorm(1e3, 3.5), plot.points = FALSE))

推荐阅读