Reputation: 141
I am using plot3d()
from rgl
to make 3D scatter plots, using three columns, of samples in a data frame. Furthermore, I am using a fourth column colorby
from my data frame (where each sample takes values, say, 10, 11, 12 as factors/levels) to color the points in the plot.
When using plot()
to make 2D plots, I first set palette(rainbow(3))
and then col = colorby
within plot()
followed by legend()
. The plot, colors and legend work fine.
However, when I repeat the last part for plot3d()
, the coloring mixes up and not the same colors are assigned to the same levels as they would be in plot()
. Moreover, if I use legend3d("topright", legend = levels(colorby), col = rainbow(3))
to create a legend, it looks the same as the 2D legend, but the coloring is clearly wrong in the 3D plot.
Where am I going wrong?
Upvotes: 3
Views: 7115
Reputation: 54237
This looks correct to me:
df <- data.frame(x=1:9, y=9:1, z=101:109, colorby=gl(3,3))
palette(rainbow(3))
plot(x~y, df, col = df$colorby)
library(rgl)
with(df, plot3d(x,y,z, col = colorby))
legend3d("topright", legend = levels(df$colorby), col = levels(df$colorby), pch=19)
Upvotes: 2