How to edit the legend of a scatter plot?

时间:2017-12-17 08:12:05

标签: r legend scatter3d

I made a plot with legend. By this code:

scatter3d(x = red, y = green, z = blue, groups = C1class$V1, surface. col = 1:21,
      grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(C1class$V1),
   col = rainbow(21), pch = 16, inset = -0.25, xpd = TRUE)

But my graph looks like this:

enter image description here

How can I edit it to look better?
Can you help me with some function to fix it?
Thanks for you help.

1 个答案:

答案 0 :(得分:0)

您没有提供数据,因此我无法准确测试您的问题, 但我认为以下示例将帮助您解决问题。

我认为您的问题源于包含参数inset = -0.25。 这就是将传奇从显示区域移开。看着你的照片, 我认为你这样做的原因是因为否则 图例覆盖了散点图的一部分。解决此问题的另一种方法是使用windowRect调整绘图区域以允许更多空间。

像你这样的情节被传说切断了。

scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5], 
    surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
   col = rainbow(3), inset = -0.25, pch = 16, xpd = TRUE)

Cropped legend

没有缩进的第二个图。现在传说与情节重叠。

scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5], 
    surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
   col = rainbow(3), pch = 16, xpd = TRUE)

Legend overlaps plot

最后,调整窗口大小以便为图例留出更多空间。

par3d(windowRect = c(100, 100, 600, 350))
scatter3d(x = iris[,1], y = iris[,2], z = iris[,3], groups = iris[,5], 
    surface.col = rainbow(3), grid = FALSE, surface = FALSE)
legend3d("right", legend = levels(iris[,5]),
   col = rainbow(3), pch = 16, xpd = TRUE)

Desired result

您可能需要以不同方式调整数据windowRec,但此设置将是一个很好的起点。