很遗憾,使用的数据集不可用,因此无法忠实再现图像并展开细节。
要创建轮廓曲面,使用 ggplot2 包和 geom_raster 函数很有趣。但这会返回一个连续的表面。
library(ggplot2)
library(dplyr)
x <- seq(1, 6, 1)
y <- seq(0, 3, 1)
grid <- expand.grid(x = x,
y = y)
m <- matrix(c(7,73,195,195,416,625,120,52,178,178,349,454,6,
83,164,164,244,0,3,52,150,150,329,330),
nrow = 4, ncol = 6, byrow = T)
m2 <- m %>%
t %>% as.data.frame()
grid <- grid %>%
dplyr::mutate(response = c(m2$V1, m2$V2, m2$V3, m2$V4))
Palet <- c("royalblue2", "orangered3", "lavenderblush3", "gold3")
ggplot2::ggplot(grid, aes(x, y, z = response)) +
geom_raster(aes(fill = response)) +
scale_fill_gradientn(colours = Palet, limits = c(0, 800)) +
theme_void() +
theme(legend.position = "bottom")
此外,可以执行数据操作并使用离散的fill。对于每个间隔,将分配一种颜色。
grid2 <- grid %>%
dplyr::mutate(cor = ifelse(response >= 0 & response < 200, 1,
ifelse(response >= 200 & response < 400, 2,
ifelse(response >= 400 & response < 600, 3,
ifelse(response >= 600, 4, "error")))))
ggplot2::ggplot(grid2, aes(x, y, z = response)) +
geom_raster(aes(fill = cor)) +
scale_fill_manual(values = Palet, labels = c("0-200", "200-400",
"400-600", "600-800"),
name = "") +
theme_void() +
theme(legend.position = "bottom")
但是,如果考虑矩阵m中的每个单元格都是强度,则可以生成3D图。
library(plot3D)
persp3D(z = m, theta = 60)
3D surface plot from 2D matrix
Plot 3D data in R
Impressive package for 3D and 4D graph - R software and data visualization