带有分类颜色的 R 3d 图

标签 r plot

我在 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)))

enter image description here

或者,您可以使用 latticecloud ,在这种情况下,您可以使用 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)))

enter image description here

关于带有分类颜色的 R 3d 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698866/

相关文章:

r - 在 map 上绘制线条 - gcIntermediate

r - 删除R的一列中具有非数字字符的行

python - matplotlib 中的正号刻度标签

r - 错误 - lognet 中的错误(x、is.sparse、ix、jx、y、权重、偏移量、alpha、nobs)= 等

r - 矩阵的特征值,假设对称

python - matlib 绘制每个条目的图表,想要每个国家/地区的合并总数

python - Matplotlib colorbar 移动第二个 x 轴

matlab - 在 Matlab 图形中绘制图例

r - 为什么 r 中关于生成 Gamma 随机变量的代码没有返回预期的输出?

r - 如何仅获取特定行的列均值?