我在 R 中有 4 列数据,看起来像这样
x y z group
group
列具有分类值,因此它是一组离散值,而其他三列是连续的。我想用
x
在 R 中制作 3d 绘图, y
, 和 z
,并且点的颜色由“组”给出。我也想给这个情节一个传奇。我怎样才能做到这一点?我对实际颜色没有特别的偏好。我想 rainbow(length(unique(group))
应该没问题。
最佳答案
这是一个使用 scatterplot3d
的示例并基于小插图中的示例
library(scatterplot3d)
# some basic dummy data
DF <- data.frame(x = runif(10),
y = runif(10),
z = runif(10),
group = sample(letters[1:3],10, replace = TRUE))
# create the plot, you can be more adventurous with colour if you wish
s3d <- with(DF, scatterplot3d(x, y, z, color = as.numeric(group), pch = 19))
# add the legend using `xyz.convert` to locate it
# juggle the coordinates to get something that works.
legend(s3d$xyz.convert(0.5, 0.7, 0.5), pch = 19, yjust=0,
legend = levels(DF$group), col = seq_along(levels(DF$group)))
或者,您可以使用
lattice
和 cloud
,在这种情况下,您可以使用 key
构造 key cloud(z~x+y, data = DF, pch= 19, col.point = DF$group,
key = list(points = list(pch = 19, col = seq_along(levels(DF$group))),
text = list(levels(DF$group)), space = 'top', columns = nlevels(DF$group)))
关于带有分类颜色的 R 3d 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698866/