向xyplot面板添加不同数据的缩写 [英] Adding abline of different data to xyplot panels

查看:62
本文介绍了向xyplot面板添加不同数据的缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R中的晶格,我已经使用factors选项绘制了一个xyplot.现在,我想向每个图添加水平斜线,但是要在另一个数据框中添加一个水平斜线.如何在对应的面板中找到这些行?

Using lattice in R, I have plotted an xyplot using the factors option. Now I want to add a horizontal abline to every plot, however out of another dataframe. How can I get these lines in their correspondent panel?

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)

推荐答案

这是我要确保值匹配的方式

Here's how i'd do it to make sure the values match up

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])
  }
)

这产生

这篇关于向xyplot面板添加不同数据的缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆