scatterplot3d是在三维空间进行数据可视化的R包,包中只含有一个函数scatterplot3d(),scatterplot3d()的用法如下:


# install.packages("scatterplot3d")
scatterplot3d(x, y=NULL, z=NULL, color=par("col"), pch=par("pch"),
    main=NULL, sub=NULL, xlim=NULL, ylim=NULL, zlim=NULL,
    xlab=NULL, ylab=NULL, zlab=NULL, scale.y=1, angle=40,
    axis=TRUE, tick.marks=TRUE, label.tick.marks=TRUE,
    x.ticklabs=NULL, y.ticklabs=NULL, z.ticklabs=NULL,
    y.margin.add=0, grid=TRUE, box=TRUE, lab=par("lab"),
    lab.z=mean(lab[1:2]), type="p", highlight.3d=FALSE,
    mar=c(5,3,4,3)+0.1, bg=par("bg"), col.axis=par("col.axis"),
    col.grid="grey", col.lab=par("col.lab"), 
    cex.symbols=par("cex"), cex.axis=0.8 * par("cex.axis"),
    cex.lab=par("cex.lab"), font.axis=par("font.axis"),
    font.lab=par("font.lab"), lty.axis=par("lty"),
    lty.grid=par("lty"), lty.hide=NULL, lty.hplot=par("lty"),
    log="", asp=NA, ...)

下面一个简单的示例给大家演示如何优雅的绘制3D 散点图:


# 绘制3D 散点图
library(scatterplot3d)

# 设置不同组别点的颜色
mtcars$pcolor[mtcars$cyl == 4] <- "red"
mtcars$pcolor[mtcars$cyl == 6] <- "blue"
mtcars$pcolor[mtcars$cyl == 8] <- "darkgreen"


# 用scatterplot3d函数绘制3D版散点图

with(mtcars, {
    s3d <- scatterplot3d(
      x = disp, 
      y = wt, 
      z = mpg,
      color = pcolor, 
      pch = 19, 
      type = "h", 
      lty.hplot = 2, 
      scale.y = .75,
      main = "3-D Scatterplot",
      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, 
          labels = row.names(mtcars), 
          pos = 4, 
          cex = .5)  
# 添加图例
legend(# 位置
       "top", 
       inset = .05,
       border = "white",
       # 为T表示水平放置图例
       horiz = T,
       # 图例框的类型
       bty="n", 
       cex=1, 
       title="Number of Cylinders",
       c("4", "6", "8"), 
       fill=c("red", "blue", "darkgreen"))
})

参考资料:

1.https://rkabacoff.github.io