如何通过 R 晶格中的数据属性缩放符号大小?

问题描述 投票:0回答:1

我想使用 Lattice 通过数据属性来缩放符号大小。在示例中,我以月份为条件。有两组(“椭圆”),每组在 1,2 和 3 小时进行三个 x-y 观察。这是一个点和线图,我想根据小时使每个椭圆的符号大小递增,以指示时间增加的方向。在下面的 xyplot 代码中,我添加了“cex=d$Hour”,但这只是为两个椭圆分配了不同的符号大小。

d <- data.table(expand.grid(Hour=1:3, Month=1:3, Ellipse=1:2))
d[, x := c(rnorm(9, mean=1, sd=1),rnorm(9, mean=2, sd=1.5))]
d[, y := c(rnorm(9, mean=1, sd=1),rnorm(9, mean=2, sd=2))]

xyplot(y ~ x|Month, d, type=c('l','p'), group = Ellipse,
    cex = d$Hour,
    auto.key=list(title="Ellipse", corner=c(0.8,0.8)))

Example showing symbol size scaled by Ellipse instead of Hour

r lattice
1个回答
0
投票

xyplot
的默认面板函数是
panel.xyplot
,当参数
panel.superpose
为非
groups
时,它会调用
NULL

所以我们读

help("panel.superpose")

panel.superpose
x
(以及可选的
y
)变量除以
groups[subscripts]
的唯一值,并使用不同的图形参数绘制每个子集。图形参数(
col.symbol
pch
等)通常作为合适的原子向量提供,但也可以是列表。当为
panel.groups
的第
i
级别调用
groups
时,每个图形参数的相应元素将传递给它。在列表形式中,各个分量本身可以是向量。

所以我们尝试图形参数的“列表形式”

cex
:

library(lattice)
set.seed(0L)

d <- expand.grid(Hour = 1:3, Month = 1:3, Ellipse = 1:2)
d[["x"]] <- c(rnorm(9, mean = 1, sd = 1), rnorm(9, mean = 2, sd = 1.5))
d[["y"]] <- c(rnorm(9, mean = 1, sd = 1), rnorm(9, mean = 2, sd = 2  ))

xyplot(y ~ x | Month,
       data = d,
       auto.key = list(title = "Ellipse", corner = c(0.8, 0.8)),
       groups = Ellipse,
       type = c("l", "p"), 
       cex = list(seq_along(unique(d[["Hour"]]))))

这似乎可以解决问题......

same image but with points increasing in size within groups

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.