我正在尝试使用包 lattice
将线段添加到 cloud 图中。
我已经阅读了晶格文档,并且找到了功能lsegments()
和panel.segments()
,但是没有找到解决方案。实际上,我不确定这些功能是否可以在通过 cloud()
功能制作的3D图中使用。我要设置x0,y0,x1和y1坐标参数。但是如何设置线段的z坐标?
我的数据:
d <- structure(list(V1 = c(-0.231868485424126, 0.260991275378284,
-0.973129576550547, 0.150224354474776, -0.743673301914161,
0.21938366130134,
0.899845151352479, 0.0808627438470515, 0.317569956044341,
-0.481908931028419
), V2 = c(-0.964917334664869, 0.629592806926283, 0.155210445495851,
-0.808297399258872, 0.38884655631618, -0.960935067863847,
0.18526488864338,
-0.701769619946431, -0.114753054406819, -0.737619553095911),
V3 = c(0.123172816523645, -0.731776230580839, 0.170083934724984,
0.569287236528258, -0.543827523817191, -0.168744198429227,
0.394912173303622, 0.707799842595184, 0.941265668938531,
0.472949444534652)), class = "data.frame", row.names = c(NA,
-10L))
我的分段向量:
from <- c(4, 1, 1, 1, 8, 3, 7, 2, 3)
to <- c(8, 10, 6, 4, 9, 5, 9, 5, 10)
我的目标在2D模式下的样子:
plot(d[,1:2])
segments(d[from,1], d[from,2], d[to,1], d[to,2],col="black", lty=3 )
到目前为止,我设法用格子做的事情:
lattice::cloud(V3~V1+V2, data = d, pch= 19,
screen=list(z = 30, x = -70, y = 0),
scales=list(arrows=FALSE, cex=0.6, col="black", font=3,
tck=0.6, distance=1) ,
drop.unused.levels = FALSE,
panel = function(...){
lattice::panel.cloud(...)
lattice::panel.segments(x0 = d[from,1], y0 = d[from,2], x1 =
d[to,1], y1 = d[to,2], z0=d[from,3], z1=d[to,3], lwd=2, col="blue" )
})
我知道没有参数z0或z1。我把它们仅作为我打算做什么的例证。即使这样,此代码也会产生以下图形:
立柱上有一条奇怪的蓝线。
其他信息:
我也阅读了以下SO问题,但找不到解决方案:
Add 3D abline to cloud plot in R's lattice package
How to plot segments or arrows with Lattice in R? (from a Wide format dataset)
Plotting a wireframe AND a cloud with lattice in R
会议信息:
R version 3.5.1 (2018-07-02)
Platform: x86_64-suse-linux-gnu (64-bit)
Running under: openSUSE Leap 15.0
Matrix products: default
BLAS: /usr/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lattice_0.20-35 MultiRNG_1.1 KODAMA_1.4
bindrcpp_0.2.2 prodlim_2018.04.18 dplyr_0.7.6
RcppMLPACK_1.0.10-6
[8] Rcpp_0.12.18
loaded via a namespace (and not attached):
[1] rstudioapi_0.7 bindr_0.1.1 magrittr_1.5 splines_3.5.1
tidyselect_0.2.4 R6_2.2.2 rlang_0.2.1 fansi_0.2.3
[9] tools_3.5.1 grid_3.5.1 checkmate_1.8.5 utf8_1.1.4
cli_1.0.0 yaml_2.2.0 survival_2.41-3 assertthat_0.2.0
[17] tibble_1.4.2 crayon_1.3.4 Matrix_1.2-14 lava_1.6.3
purrr_0.2.5 glue_1.3.0 compiler_3.5.1 pillar_1.3.0
[25] backports_1.1.2 BBmisc_1.11 pkgconfig_2.0.1