2015-10-16 37 views
0

我在CSV文件中的一些数据,并想画一个4D图形。 x,y,z轴分别是文件中的一列。第四维是文件中另一列值的颜色。我如何在R中同时获得x,y,z和颜色?使用R绘制4D数字

+0

你可能想看看'演示(persp)'看到的例子如何能够在基础R.做了'hist3d()'从PLOT3D格式封装的功能也可能是有用的。这个链接]( H TTP://stackoverflow.com/a/31247657/4770166)示出了一个例子。 – RHertel

回答

2

您将能够使与根据数据集中的另一个变量编码彩色信息的三维图。这取决于你是否需要表面或散点图。例如,三维散点图封装(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 scatterplot with color information

可以找到的示例的列表,包括上面的一个,here

+0

答案是非常有帮助的。谢谢,肖恩。 :) –