R语言使用ggcube包轻松绘制文章中的各类3D图形

最近经粉丝推荐发现了一个有趣的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图形可以考虑一下。

相关推荐
唐钰小宝B2 小时前
深度解读 JDK 8、JDK 11、JDK 17 和 JDK 21 的区别
java·开发语言·windows
知识分享小能手3 小时前
C学习教程,从入门到精通,C语言指针 —— 详细知识点总结(10)
c语言·开发语言·学习
熊猫钓鱼>_>3 小时前
开源鸿蒙平台 KMP 三方库 kotlin-csv 适配全流程:从编码假设到沙箱真机互通验证
开发语言·人工智能·华为·kotlin·harmonyos·鸿蒙
张张123y3 小时前
# 后端第七课:FastAPI 实战入门(用刚学的 Python 写第一个 API 接口)
开发语言·python·mysql·api·fastapi
小陈不好吃3 小时前
Java 泛型与类型擦除:从 PECS 原则到 TypeToken 绕过
java·开发语言·windows·pecs 原则·typetoken绕过
Shaoshing3 小时前
存储引擎与架构
java·开发语言·架构
额额额对了3 小时前
Linux 驱动开发:基于 platform 总线 + misc 设备的 DS18B20 单总线温度驱动
linux·c语言·开发语言·驱动开发·嵌入式硬件
devilnumber4 小时前
Java 代理模式(简单理解回顾)
java·开发语言·代理模式