简体   繁体   English

在R中的3D格子图中使用2个分组变量

[英]Use 2 grouping variables in a 3D lattice plot in R

I'm trying to produce a 3D cloud plot using the lattice package in R. I'd like colors and symbols to be grouped by two different factors on the plot. 我正在尝试使用R中的点阵软件包生成3D云图。我希望​​颜色和符号可以按图上的两个不同因素进行分组。 I found this example of how to do this using xyplot , but i'm having trouble adapting it for the cloud function (see code for Fig. 9.2): 我找到了使用xyplot进行此操作的xyplot ,但是我无法将其调整为cloud功能(请参见图9.2的代码):

http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=09;figure=09_02;theme=stdBW;code=right http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=09;figure=09_02;theme=stdBW;code=right

In particular, there is a "subscripts" argument in panel.xyplot that is not present in panel.cloud , and i'm not sure how to change that part of my code. 特别是, panel.xyplot中没有出现在panel.cloud的“下标”参数,我不确定如何更改代码的这一部分。

Here's a self contained example of what i'm doing using the mtcars data set. 这是我使用mtcars数据集正在做的一个自包含示例。 The commented out code in the "panel = " part of the cloud call is what I want to fix. 我要修复的是在cloud调用的“ panel =”部分中注释掉的代码。 I'd like to color the data points by the variable cyl , and use different symbols to denote different clusters (from a kmeans analysis). 我想用变量cyl为数据点着色,并使用不同的符号表示不同的聚类(来自kmeans分析)。 Any suggestions are much appreciated. 任何建议,不胜感激。

# data
data(mtcars)

# kmeans
set.seed(321)
km <- kmeans(x = mtcars[, c(1, 3:7)], centers = 3, nstart = 1000)
mtcars$cluster <- factor(km$cluster)
mtcars$cyl <- factor(mtcars$cyl)

# pca
form <- formula(c("~", paste0(colnames(mtcars[, c(1, 3:7)]), collapse = "+")))
pca <- prcomp(form, data = mtcars[, c(1, 3:7)])

library(lattice)
library(RColorBrewer)

# set colors and points
colr <- brewer.pal(3, "Set1") 
pchr <- 0:2 

# set plot options
par.set <- list(axis.line = list(col = "transparent"),
                clip = list(panel = "off"),
                superpose.symbol = list(pch = pchr,
                                        col = colr))

# cloud plot
cloud(x[, "PC3"] ~ x[, "PC1"] * x[, "PC2"], 
     data = pca,
     zlab = "PC3", xlab = "PC1", ylab = "PC2",
     cex = 1, 
     groups = mtcars$cluster,
     type = "p",
     main = "3 cluster solution",
     screen = list(z = 20, x = -70, y = 3),
     par.settings = par.set,
     scales = list(col = "black"),
     #panel = function(x, y, z, subscripts, ...) {
     #          pch <- pchr[mtcars$cluster[subscripts]]
     #          col <- colr[mtcars$cyl[subscripts]]
     #          panel.cloud(x, y, z, pch = pch, col = col) },
     key = list(points = list(type = "p", pch = pchr, col = "black", cex = 1.2),
                text = list(paste("Cluster", levels(mtcars$cluster))),
                points = list(type = "p", pch = 1, col = colr, cex = 1.2),
                text = list(paste("Cyl", levels(mtcars$cyl))),
                columns = 1))

You can omit the groups argument and substitute a pch and a col argument: 您可以忽略groups参数,而用pch和col参数代替:

  ... , pch=mtcars$cyl, col=mtcars$cluster, ...

(Just to clarify why I wrote there was not an "x"-object: In R a named element of a list is not considered an object . In your case you were able to access that element because of your use of the data argument which allowed access to that element of the object "pca" within a local environment. I incorrectly surmised that you would not be able to use expressions of the form x[,"PC3"] inside a formula. As it turned out you were able to get them evaluated correctly.) (只是为了澄清为什么我写了一个没有“ x”对象的原因:在R中,列表的命名元素不被视为object 。在这种情况下,由于使用了data参数,因此能够访问该元素。允许我在本地环境中访问对象“ pca”的元素。我错误地推测您将无法在公式中使用x[,"PC3"]形式的表达式。事实证明,您能够让他们正确评估。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM