使用R绘制4d图形
Use R to draw a 4d figure
我在 CSV 文件中有一些数据,想绘制 4d 图形。 x、y、z 轴分别是文件中的一列。第 4 个维度是文件中另一列值的颜色。如何在 R 中获得包含 x、y、z 和颜色的绘图?
您将能够使用根据数据集中的另一个变量编码的颜色信息制作 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"))
})
屈服
您可以找到示例列表,包括上面的示例,here。
我在 CSV 文件中有一些数据,想绘制 4d 图形。 x、y、z 轴分别是文件中的一列。第 4 个维度是文件中另一列值的颜色。如何在 R 中获得包含 x、y、z 和颜色的绘图?
您将能够使用根据数据集中的另一个变量编码的颜色信息制作 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"))
})
屈服
您可以找到示例列表,包括上面的示例,here。