在 Lattice 的面板函数中使用 ifelse 或 if 循环不适用于 panel.xyplot
Using an ifelse or if loop in the panel function in Lattice doesn't work with panel.xyplot
我正在尝试使用 lattice
绘制一个绘图,其中点的颜色和形状都会根据数据中的变量发生变化。当我自己设置 col
和 pch
值并且不要将 panel.xyplot
放在 ifelse
语句中时,这工作正常:
#Setup
library(cluster)
library(lattice)
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
pch <- ifelse(V8_var[subscripts] < 50,
2,
6)
panel.xyplot(x,y,type='p',
col = col,
pch = pch)
})
然而,当我在 ifelse
或 if
循环中使用 panel.xyplot
时,这不起作用:
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
ifelse(
V8_var[subscripts] < 50,
panel.xyplot(x,y,type='p',
col = col,
pch = 2),
panel.xyplot(x,y,type='p',
col = col,
pch = 6)
)
})
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
if(V8_var[subscripts] < 50){
panel.xyplot(x,y,type='p',
col = col,
pch = 2)
} else {
panel.xyplot(x,y,type='p',
col = col,
pch = 6)
}
})
使用if
语句也会给出警告:
Warning message:
In if (V8_var[subscripts] < 50) { :
the condition has length > 1 and only the first element will be used
解决这个问题的方法是通过 if
或 ifelse
语句获取变量值,然后将它们插入任何 if
语句之外的 panel.xyplot
,但是我不明白为什么 ifelse
语句在 panel.xyplot
包含在其中时不能像我预期的那样工作?
会话信息:
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lattice_0.20-41 cluster_2.1.0
loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2 grid_4.0.2
panel.xyplot 不能作为 ifelse 的参数。计算 pch 的方式与代码计算 col:
的方式相同
library(cluster)
library(lattice)
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50, 'blue', 'orange')
pch <- ifelse(V8_var[subscripts] < 50, 2, 6)
panel.xyplot(x,y,type='p',
col = col,
pch = pch)
})
尽管在这种情况下您甚至不需要面板功能:
xyplot(V8 ~ V7,
data = flower,
col = ifelse(flower$V8 < 50, "blue", "orange"),
pch = ifelse(flower$V8 < 50, 2, 6))
我正在尝试使用 lattice
绘制一个绘图,其中点的颜色和形状都会根据数据中的变量发生变化。当我自己设置 col
和 pch
值并且不要将 panel.xyplot
放在 ifelse
语句中时,这工作正常:
#Setup
library(cluster)
library(lattice)
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
pch <- ifelse(V8_var[subscripts] < 50,
2,
6)
panel.xyplot(x,y,type='p',
col = col,
pch = pch)
})
然而,当我在 ifelse
或 if
循环中使用 panel.xyplot
时,这不起作用:
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
ifelse(
V8_var[subscripts] < 50,
panel.xyplot(x,y,type='p',
col = col,
pch = 2),
panel.xyplot(x,y,type='p',
col = col,
pch = 6)
)
})
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50,
'blue',
'orange')
if(V8_var[subscripts] < 50){
panel.xyplot(x,y,type='p',
col = col,
pch = 2)
} else {
panel.xyplot(x,y,type='p',
col = col,
pch = 6)
}
})
使用if
语句也会给出警告:
Warning message:
In if (V8_var[subscripts] < 50) { :
the condition has length > 1 and only the first element will be used
解决这个问题的方法是通过 if
或 ifelse
语句获取变量值,然后将它们插入任何 if
语句之外的 panel.xyplot
,但是我不明白为什么 ifelse
语句在 panel.xyplot
包含在其中时不能像我预期的那样工作?
会话信息:
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lattice_0.20-41 cluster_2.1.0
loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2 grid_4.0.2
panel.xyplot 不能作为 ifelse 的参数。计算 pch 的方式与代码计算 col:
的方式相同library(cluster)
library(lattice)
xyplot(V8 ~ V7,
data = flower,
V8_var = flower$V8,
panel = function(x, y, ..., V8_var, subscripts) {
col <- ifelse(V8_var[subscripts] < 50, 'blue', 'orange')
pch <- ifelse(V8_var[subscripts] < 50, 2, 6)
panel.xyplot(x,y,type='p',
col = col,
pch = pch)
})
尽管在这种情况下您甚至不需要面板功能:
xyplot(V8 ~ V7,
data = flower,
col = ifelse(flower$V8 < 50, "blue", "orange"),
pch = ifelse(flower$V8 < 50, 2, 6))