最近经粉丝推荐发现了一个有趣的R包,可以专业绘制3D图形。ggcube包ggcube是ggplot2的3D绘图扩展。它提供了三维几何属性,以及一个可以构建3D可视化的3D坐标系统 使用熟悉的 ggplot2 语法:将数据映射为美观,添加 叠加,并通过音阶、指南和主题自定义结果,3D剧情非常适合探索、交流和艺术。

下面我来演示一下,绘制3D图形主要是需要三维数据,就是长宽高,ggcube包不擅长生成数据,主要是通过已经生成好的数据来绘图。
下面我来演示一下,数据使用的是R自带的汽车数据
r
library(ggcube)
mpg<-mpg

这是R自带的汽车数据,主要是介绍一些汽车的品牌和参数有兴趣的自己看下R包解释,咱们先来画个3D散点图,非常容易,就是在平常画法上加上一行语法
r
ggplot(mpg, aes(x = displ, y = hwy, z = drv, color = class)) +
geom_point() +
coord_3d()

coord_3d这个语法控制3D场景的投影方式 二维。旋转通过三个角度表示------(倾斜 绕x轴)、(绕y轴倾斜)和(绕z轴旋转)
r
ggplot(mpg, aes(displ, hwy, drv, color = class)) +
geom_point() +
coord_3d(pitch = 0, roll = 60, yaw = 0, dist = 1.4,
ratio = c(2, 1, 1), panels = "all") +
theme(panel.border = element_rect(color = "black"),
panel.foreground = element_rect(alpha = .1))

3D散点图还可以进一步扩展
r
ggplot(mpg, aes(x = displ, y = hwy, z = drv, fill = class)) +
geom_point_3d(size = 3, shape = 21, color = "black", stroke = .1,
ref_lines = TRUE, ref_faces = c("ymax", "xmax")) +
coord_3d()

下面绘制一个复杂一点的,使用ggcube包自带的mountain数据,数据是下面这个样子

r
ggplot(mountain, aes(x, y, z)) +
geom_surface_3d(aes(fill = z, color = z)) +
scale_fill_viridis_c() + scale_color_viridis_c() +
coord_3d(ratio = c(1.5, 2, 1), expand = FALSE, panels = "zmin") +
light(direction = c(1, 0, 0)) +
guides(fill = guide_colorbar_3d()) +
theme_light()

还可以绘制3D路径图,先生成一个自定义数据

r
spiral <- data.frame(x = x, y = sin(x), z = cos(x), time = 1:length(x))
ggplot(spiral, aes(x, y, z, color = time)) +
geom_path_3d() +
scale_color_gradientn(colors = c("blue", "purple", "red", "orange")) +
coord_3d() +
theme_light()

使用geom_col_3d函数生成3D柱状图,创建带有自动分帧功能的3D直方图, 并渲染立方体数组
r
ggplot(iris, aes(Species, Sepal.Length, fill = Species)) +
geom_bar_3d(bins = 20, width = c(.5, 1)) +
coord_3d(scales = "fixed", ratio = c(1, 3, .25), yaw = 60) +
scale_z_continuous(expand = c(0, 0)) +
theme(legend.position = "none")

geom_hull_3d函数绘制球形的3D图
r
library(geometry)
ggplot(sphere_points, aes(x, y, z)) +
geom_hull_3d(method = "convex", fill = "#9e2602", color = "#5e1600") +
coord_3d()

还可以对球体添加光照效果
r
p <- ggplot(sphere_points, aes(x, y, z)) +
geom_hull_3d(fill = "#9e2602", color = "#5e1600") +
coord_3d()
p + light(method = "direct", mode = "hsl", direction = c(0, 0, 1))

stat_distributions_3d函数计算3D核密度图,对每个群进行估算,并将其排列为山脊线曲面
r
ggplot(iris, aes(y = Sepal.Length, x = Species, fill = Species)) +
stat_distributions_3d() +
scale_z_continuous(expand = expansion(mult = c(0, NA))) +
coord_3d() +
theme(legend.position = "none")

文字渲染3D效果
r
df <- expand.grid(x = c("H", "B"), y = c("a", "o", "u"), z = c("g", "t"))
df$label <- paste0(df$x, df$y, df$z)
ggplot(df, aes(x, y, z, label = label, fill = x)) +
geom_text_3d(method = "polygon", facing = "zmax",
size = 5, weight = "bold") +
coord_3d(scales = "fixed", light = NULL)

二维和三维的混合图形
r
ggplot(iris, aes(Sepal.Length, Sepal.Width, Petal.Length,
color = Species, fill = Species)) +
coord_3d() + xlim(4, 8) +
stat_density_2d(position = position_on_face(faces = "zmin", axes = c("x", "y")),
geom = "polygon", alpha = .1, linewidth = .25) +
geom_hull_3d(position = position_on_face("ymax"), alpha = .5) +
geom_point_3d(shape = 21, color = "black", stroke = .25)

生成一些可以拖动的3D控件,这个需要一点时间
r
p <- ggplot(mountain, aes(x, y, z)) +
geom_contour_3d(fill = "black", color = "white", linewidth = .5, light = "none") +
coord_3d(ratio = c(1.5, 2, 1), zoom = 1.25) +
theme_void()
orbit_3d(p, yaw = c(360, 0), roll = c(-90, 0), n = c(24, 8),
start = c(yaw = 300, roll = -60))

R包生成这个图形可以转动,我这里截图肯定不能了。
最后总结一下,这是一个强大而使用的R包,要是绘制3D图形可以考虑一下。