向R中的图添加不同的曲线 [英] adding different curves to plots in R
本文介绍了向R中的图添加不同的曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以使用 lattice
和 latticeExtra
将相同曲线作为 layer
添加到每个绘图窗格中R包(请参见打击).
I know I can add the same curve to each plot pane as a layer
using lattice
and latticeExtra
R packages (see blow).
但是假设我们想向每个绘图窗格添加不同曲线.
But suppose we wanted to add different curves to each plot pane.
例如,在下面的示例中,我想知道如何仅将 v1
添加到顶部图,将 v2
添加到左下图和 v3
到右下图?
For example, in the below example, I wonder how to add v1
only to the top plot, v2
to the bottom-left plot, and v3
to the bottom-right plot?
library(lattice)
library(latticeExtra)
set.seed(24)
v1 <- density(rnorm(1e3, 3.5))
v2 <- density(rnorm(1e3, 3))
v3 <- density(rnorm(1e3, 2.75))
foo <- xyplot((1:32*.01)~wt|gear , data = mtcars)
foo + layer(panel.polygon(v1, col = 2, alpha = 0.3))
推荐答案
这里是一个选择
foo +
layer(if(panel.number() == 3) {
panel.polygon(v1, col = 2, alpha = 0.3)
} else if(panel.number() == 1) {
panel.polygon(v2, col = 2, alpha = 0.3)
} else{
panel.polygon(v3, col = 2, alpha = 0.3)
})
library(lattice)
library(latticeExtra)
set.seed(24)
v1 <- density(rnorm(1e3, 3.5))
v2 <- density(rnorm(1e3, 3))
v3 <- density(rnorm(1e3, 2.75))
foo <- xyplot((1:32*.01)~wt|gear , data = mtcars)
这篇关于向R中的图添加不同的曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文