透视图

作者

[编辑] 郑虎;

[审核] .

注记

Hiplot 网站

本页面为 Hiplot Perspective 插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:

https://hiplot.cn/basic/perspective?lang=zh_cn

三维透视图是一种能将一个矩阵中包含的高等数值用曲面连接起来的三维立体图形。

环境配置

  • 系统: Cross-platform (Linux/MacOS/Windows)

  • 编程语言: R

  • 依赖包: data.table; jsonlite; shape; ggplotify

# 安装包
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("shape", quietly = TRUE)) {
  install.packages("shape")
}
if (!requireNamespace("ggplotify", quietly = TRUE)) {
  install.packages("ggplotify")
}

# 加载包
library(data.table)
library(jsonlite)
library(shape)
library(ggplotify)

数据准备

载入数据为一个矩阵。

# 加载数据
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/perspective/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)

# 整理数据格式
data <- as.matrix(data)
col <- drapecol(data)

# 查看数据
head(data[,1:5])
      V1  V2  V3  V4  V5
[1,] 100 101 102 103 104
[2,] 100 101 102 103 104
[3,] 101 102 103 104 105
[4,] 101 102 103 104 105
[5,] 101 102 103 104 105
[6,] 101 102 103 104 105

可视化

# 透视图
p <- as.ggplot(function() {
  persp(as.matrix(data),
    theta = 45, phi = 20,
    expand = 0.5,
    r = 180, col = col,
    ltheta = 120,
    shade = 0.5,
    ticktype = "detailed",
    xlab = "X", ylab = "Y", zlab = "Z",
    border = "black" # could be NA
  )
  title("Perspective Plot", line = 0)
})

p
图 1: 透视图

清晰观测一个山峰的全貌。