【问题标题】:How to give a color to each x value in a 3D scatterplot in R如何为R中的3D散点图中的每个x值赋予颜色
【发布时间】:2020-02-11 23:11:40
【问题描述】:

我需要一起分析 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 值都以不同的颜色绘制? 谢谢!

【问题讨论】:

    标签: r colors scatter-plot


    【解决方案1】:

    当您将 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]
    

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2022-01-14
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 2014-10-09
      相关资源
      最近更新 更多