如何在R中的3D散点图中为每个x值赋予颜色

时间:2019-10-15 09:48:33

标签: r colors scatter-plot

我需要一起分析3组数据。我正在使用scatterplot3d分析这些数据。 我需要给x轴上的每个值赋予不同的颜色。

The value on the x axis is Terreno (Soil) and goes from -4 to +6.
The value on the y axis is P.I.
The value on the z axis is PERSE.TOTALI (Total lost).

我可以创建3D散点图,但不能为x轴上的每个值赋予颜色,该如何进行?     谢谢

    #here are the data I'm trying to plot

    dput(head(dati3,25))
    structure(list(Terreno = c(-4L, -4L, -4L, 5L, -4L, 3L, 6L, -4L,
    5L, -4L, 3L, -4L, 5L, 3L, 5L, -4L, -4L, 3L, -4L, 3L, 3L, -4L,
    -4L, -4L, 3L), P.I. = c(1, 0, 1, 2, 0, 2, 2, 2, 2, 1.5, 2, 1.5,
    1, 2, 1.5, 1, 0, 0, -0.5, 2, 1, 1, 1, 1, 1), PERSE.TOTALI = c(0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0)), row.names = c(NA, 25L), class = "data.frame")



    #this is the code I'm using for the plot

    scatterplot3d(Terreno, P.I., PERSE.TOTALI,pch=16,highlight.3d = T, type="h")

我应该在代码中插入什么以使每个x值以不同的颜色绘制?     谢谢!

1 个答案:

答案 0 :(得分:0)

highlight.3d设置为true时,颜色是自动的,您不能更改它。尽管如果您愿意使用x以外的颜色,则可以使用以下代码:

scatterplot3d(Terreno, P.I., PERSE.TOTALI, pch= 16, type= "h",
              color= as.numeric(as.factor(x)))

color可以将具有相同数据长度的任何矢量作为参数,并且该矢量的不同值将为数据点提供不同的颜色,因此在代码中,该矢量的值来自矢量x和等于x。如果您决定使用其他颜色的调色板,则可以使用如下代码:

color= palette[as.numeric(as.factor(x)))]

或者也许只是:

color= palette[x+5]