gpt4 book ai didi

r - 将不同数据的 abline 添加到 xyplot 面板

转载 作者:行者123 更新时间:2023-12-01 13:42:22 25 4
gpt4 key购买 nike

使用 R 中的 lattice,我使用 factors 选项绘制了一个 xyplot。现在我想为每个图添加一个水平 abline,但是在另一个数据框中。我怎样才能在他们对应的面板中得到这些行?

EDIT - example:

MyData <- data.frame(
v1 = 1:50,
v2 = rnorm(50,20,5),
v3 = c(rep("one", 10), rep("two", 10), rep("three", 10), rep("four", 10), rep("five", 10)))


require(latticeExtra)

xyplot(
v1 ~ v2 | v3,
data = MyData,
layout = c(5, 1),
scales = list(
x = list(relation="free"),
y = list(relation="same")
)
)

# Now the horizontal lines I want to draw
lineData <- c(30,30,20,20,40)
# each for one column, from left to right; another option would be
lineData <- data.frame(
v3 = c("one","two","three","four","five"),
height = c(30,30,20,20,40)
)

# I know this will not work for each correspondent panel, separately...
abline(h = lineData)

最佳答案

下面是我将如何确保值匹配

lineData <- data.frame(
v3 = c("one","two","three","four","five"),
height = c(30,30,20,20,40)
)

xyplot(
v1 ~ v2 | v3,
data = MyData,
layout = c(5, 1),
scales = list(
x = list(relation="free"),
y = list(relation="same")
),
panel = function(x, ...) {
level <- dimnames(trellis.last.object())[["v3"]][packet.number()]
panel.xyplot(x, ...);
panel.abline(h = lineData$height[lineData$v3==level])
}
)

这产生

enter image description here

关于r - 将不同数据的 abline 添加到 xyplot 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com