将图例添加到R

时间:2018-01-11 13:21:26

标签: r plot visualization legend

我可视化集群结果如下所示:

plot3d(RFM[,2:4],col= RFM$CLASS)

enter image description here

现在如何为具有相应颜色的群集标签(CLASS)添加图例?

我的数据如下:

enter image description here

编辑:

我已经尝试过legend3d和text3d,我可以添加图例,但问题是我不能确定图例的颜色是否与形状的颜色相同,因为它们对应于给定的类!,怎么能我是以正确的方式做到的?

我的解决方案: 我得到了一个对我有用的解决方案,因为我说我想确保颜色在图例和形状上保持相同,所以我这样做了:

   plot3d(RFM[RFM$CLASS==1,2:4],col = "red" )
plot3d(RFM[RFM$CLASS==2,2:4],col = "blue", add=T )
plot3d(RFM[RFM$CLASS==3,2:4],col = "green", add=T )
plot3d(RFM[RFM$CLASS==4,2:4],col = "cyan", add=T )
plot3d(RFM[RFM$CLASS==5,2:4],col = "yellow", add=T )

legend3d("topright", legend = paste('Type', c('1','2','3','4','5')), pch = 16, col = c("red","blue","green","cyan","yellow")  , cex=1, inset=c(0.02))

2 个答案:

答案 0 :(得分:0)

我认为你可以使用legend3d

legend3d("topright", legend = paste('Type', c('A', 'B', 'C', 'D', 'E')), pch = 16, col = RFM$CLASS, cex=1, inset=c(0.02)) 

答案 1 :(得分:0)

这里是我发现这个问题的解决方案,正如我所说,我想确保颜色在图例和形状上保持相同,:

lot3d(RFM[RFM$CLASS==1,2:4],col = "red" )
plot3d(RFM[RFM$CLASS==2,2:4],col = "blue", add=T )
plot3d(RFM[RFM$CLASS==3,2:4],col = "green", add=T )
plot3d(RFM[RFM$CLASS==4,2:4],col = "cyan", add=T )
plot3d(RFM[RFM$CLASS==5,2:4],col = "yellow", add=T )

legend3d("topright", legend = paste('Type', c('1','2','3','4','5')), pch = 16, col = c("red","blue","green","cyan","yellow")  , cex=1, inset=c(0.02))