我在CSV文件中有一些数据,想要绘制一个4d图形。 x,y,z轴分别是文件中的一列。第四维是与文件中另一列的值相对应的颜色。如何在R?
中获得包含x,y,z和颜色的图答案 0 :(得分:2)
您将能够根据数据集中的另一个变量制作带有颜色信息的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"))
})
产生
您可以找到一个示例列表,包括上面的示例here。