3D Scatterplot function in R with groups
<p>So I've been working on a scatter plot for some data that I have. I used to be able to get the scatter plot function to work, but now I can't and I don't understand what my error is. My data looks has 5 values and a column that assigns each to a cluster (I used k-means in this particular case). </p>

<pre><code>  closedmi  uncertin  certknow  sourknow  justknow  fit3.cluster
1 3.166667  6.125     2.571429  4.500     3.375     1
2 3.666667  4.250     3.428571  4.000     4.750     2
3 1.833333  5.750     1.428571  3.375     2.125     2
4 3.500000  4.500     1.857143  4.250     3.125     3
</code></pre>

<p>I'm looking to try to plot my data in 3 dimensions using the first three principle components and see the clusters. Here is my code to find the principal components, and then attach the cluster column to the principle components into a new data frame. </p>

<pre><code>#Find the 5 principal components of the data matrix
pcdf &lt;- princomp(pre2, cor=T, score=T)
pre4 &lt;- data.frame(pcdf$scores, cluster=fit3$cluster)

#Making a 3D plot of the Solution
scatter3d(pre4$Comp.1, pre4$Comp.2, pre4$Comp.3, groups=pre4$cluster, 
          surface=FALSE, grid=FALSE, ellipsoid=TRUE)
</code></pre>

<p>So then try to use scatter3d to plot the individuals using the cluster column as a grouping factor and I end up with an error. I've been using <a href="http://www.sthda.com/english/wiki/amazing-interactive-3d-scatter-plots-r-software-and-data-visualization" rel="nofollow">this source</a> for the code to get the right syntax, but I still end up with the error.</p>

<blockquote>
  <p>Error in scatter3d.default(pre4$Comp.1, pre4$Comp.2, pre4$Comp.3, groups = pre4$cluster: groups variable must be a factor</p>
</blockquote>

<p>but it is. It's in the data frame, I can call the column using pre4$cluster. Is there some formatting or syntax error I can't see? Am I just going mad?</p>

<p>I was able to get this to work just last week and now I'm not able to. I know I can use plot3d to get the visualization, but I like the visualization better using scatter3d and would like to be able to use it.</p>