Note that there are some explanatory texts on larger screens.

plurals
  1. POUse 2 grouping variables in a 3D lattice plot in R
    text
    copied!<p>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. I found this example of how to do this using <code>xyplot</code>, but i'm having trouble adapting it for the <code>cloud</code> function (see code for Fig. 9.2):</p> <p><a href="http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=09;figure=09_02;theme=stdBW;code=right" rel="nofollow">http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=09;figure=09_02;theme=stdBW;code=right</a></p> <p>In particular, there is a "subscripts" argument in <code>panel.xyplot</code> that is not present in <code>panel.cloud</code>, and i'm not sure how to change that part of my code.</p> <p>Here's a self contained example of what i'm doing using the <code>mtcars</code> data set. The commented out code in the "panel = " part of the <code>cloud</code> call is what I want to fix. I'd like to color the data points by the variable <code>cyl</code>, and use different symbols to denote different clusters (from a kmeans analysis). Any suggestions are much appreciated.</p> <pre><code># data data(mtcars) # kmeans set.seed(321) km &lt;- kmeans(x = mtcars[, c(1, 3:7)], centers = 3, nstart = 1000) mtcars$cluster &lt;- factor(km$cluster) mtcars$cyl &lt;- factor(mtcars$cyl) # pca form &lt;- formula(c("~", paste0(colnames(mtcars[, c(1, 3:7)]), collapse = "+"))) pca &lt;- prcomp(form, data = mtcars[, c(1, 3:7)]) library(lattice) library(RColorBrewer) # set colors and points colr &lt;- brewer.pal(3, "Set1") pchr &lt;- 0:2 # set plot options par.set &lt;- 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 &lt;- pchr[mtcars$cluster[subscripts]] # col &lt;- 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)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload