0

我在 CSV 文件中有一些数据,想绘制一个 4d 图形。x、y、z 轴分别为文件中的一列。第 4 维是与文件中另一列的值对应的颜色。如何在 R 中同时获得 x、y、z 和颜色的图?

4

1 回答 1

2

您将能够使用根据数据集中的另一个变量编码的颜色信息制作 3D 图。这取决于您是否需要表面或散点图。例如,一个 3D 散点图包(使用数据集install.packages("scatterplot3d")将导致mtcars

library(scatterplot3d)
# create column indicating point color
mtcars$pcolor[mtcars$cyl==4] <- "red"
mtcars$pcolor[mtcars$cyl==6] <- "blue"
mtcars$pcolor[mtcars$cyl==8] <- "darkgreen"
with(mtcars, {
    s3d <- scatterplot3d(disp, wt, mpg,        # x y and z axis
                  color=pcolor, pch=19,        # circle color indicates no. of cylinders
                  type="h", lty.hplot=2,       # lines to the horizontal plane
                  scale.y=.75,                 # scale y axis (reduce by 25%)
                  main="3-D Scatterplot Example 4",
                  xlab="Displacement (cu. in.)",
                  ylab="Weight (lb/1000)",
                  zlab="Miles/(US) Gallon")
     s3d.coords <- s3d$xyz.convert(disp, wt, mpg)
     text(s3d.coords$x, s3d.coords$y,     # x and y coordinates
          labels=row.names(mtcars),       # text to plot
          pos=4, cex=.5)                  # shrink text 50% and place to right of points)
# add the legend
legend("topleft", inset=.05,      # location and inset
    bty="n", cex=.5,              # suppress legend box, shrink text 50%
    title="Number of Cylinders",
    c("4", "6", "8"), fill=c("red", "blue", "darkgreen"))
})

屈服

带有颜色信息的 3d 散点图

您可以在此处找到示例列表,包括上述示例。

于 2015-10-16T01:11:30.840 回答