# ggplot2

**ggplot2** 是一个 R 包，用于以声明式方式创建统计图形，其基础是图形语法：用户提供数据，描述变量如何映射到视觉美学，再添加图层、标度、分面和坐标系，由包来处理绘图细节<sup>[1](https://ggplot2.tidyverse.org/)</sup>。它是 R 生态系统中使用最广泛的包之一，每年从 CRAN 下载约 2770 万次，有近 5000 个其他包依赖于它<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup>。

| 关键事实 | 详情 |
|---|---|
| 模型 | 声明式、分层的图形语法；图形通过 `+` 运算符由数据、美学、几何对象、统计变换、标度、分面、坐标和主题组装而成<sup>[1](https://ggplot2.tidyverse.org/)</sup><sup> • </sup><sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup> |
| 起源 | 由哈德利·威克姆（Hadley Wickham）于 2005 年启动；理论基础是威尔金森（Wilkinson）2005 年的《图形语法》，后者又建立在贝尔坦（Bertin，1983）的基础上<sup>[4](https://ggplot2-book.org/introduction)</sup><sup> • </sup><sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup> |
| 使用情况 | 已有超过 10 年历史，数十万人用它绘制了数百万张图<sup>[5](https://github.com/tidyverse/ggplot2/)</sup> |
| 下载量 | 每年约 2770 万次 CRAN 下载（约每天 7.6 万次，每月 230 万次），在 CRAN 包中排名第 5<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup> |
| 依赖足迹 | 4955 个包直接依赖它（393 个 Depends，4562 个 Imports），另有 1961 个间接关系；安装它会额外引入 16 个包，源码压缩包共 7.6 MB<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup> |
| 要求 | R 4.1 或更高版本；导入 cli、gtable、isoband、rlang、S7、scales、vctrs、withr 等<sup>[6](https://cran.r-project.org/web/packages/ggplot2/refman/ggplot2.html)</sup> |
| 最新主要版本 | 4.0.0，2025-09-11 在 CRAN 发布，用 S7 替换了包中的 S3 部分<sup>[7](https://ggplot2.tidyverse.org/news/)</sup><sup> • </sup><sup>[8](https://tidyverse.org/blog/2025/09/ggplot2-4-0-0/)</sup> |

## 图形语法

其理论基础是威尔金森、阿南德（Anand）和格罗斯曼（Grossman）2005 年的著作《图形语法》，该书本身又建立在贝尔坦（1983）的基础上；它描述了所有统计图形背后的基本特征<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup>。哈德利·威克姆在 2010 年发表于《计算与图形统计杂志》的一篇论文中将其重新表述为一种*分层语法*<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup>。

在分层语法中，一张图由五个映射组件构成：图层、标度、坐标系、分面和主题<sup>[4](https://ggplot2-book.org/introduction)</sup>。每个图层结合五样东西：一个数据集、一组美学映射、一个统计变换（stat）、一个几何对象（geom）和一个位置调整<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup>。标度控制数据值如何变成视觉属性，如位置、颜色和大小；分面将数据拆分为面板；坐标系可以包含激进的变换，例如将位置转换为极坐标<sup>[9](https://ggplot2-book.org/internals.html)</sup>。

威克姆的语法与威尔金森的不同之处在于组件的排列方式、默认值层级体系的发展，以及它嵌入在编程语言 R 之中<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup>。原始的冗长语法辅以 `qplot`（快速绘图），后者做出很强的假设以减少输入，便于快速实验<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup>。

## 图形如何构建

当一个 ggplot2 对象被打印时，渲染分四步进行<sup>[9](https://ggplot2-book.org/internals.html)</sup>：

1. `ggplot_build()` 将每个图层的数据准备成适合绘图的标准化格式。
2. `ggplot_gtable()` 将准备好的数据转换为图形元素，存储在 gtable 这一网格布局对象中。
3. grid 包在当前图形设备上绘制该 gtable。
4. 原 ggplot 对象以不可见方式返回。

ggplot2 本身不执行任何底层绘制；它的职责在 gtable 对象创建完成时即告结束，所有绘制都由 grid 连同当前图形设备完成<sup>[9](https://ggplot2-book.org/internals.html)</sup>。图层数据可以直接提供给图层，可以从 `ggplot()` 调用中继承，也可以由应用于全局数据的函数生成，并且会被附加一个 PANEL 列，将每一行数据链接到特定的分面面板，该列必须在渲染过程中保留<sup>[9](https://ggplot2-book.org/internals.html)</sup>。在 gtable 构建期间，坐标系会变换和归一化位置数据，包括将位置转换为极坐标等激进变换；统计变换和位置调整在此步骤之前完成工作<sup>[9](https://ggplot2-book.org/internals.html)</sup>。

## 历史与发展

ggplot2 的工作始于 2005 年，目标是结合 base 和 lattice 图形的优点，并通过一个能够生成任何类型统计图形的稳健底层模型超越两者。<sup>[4](https://ggplot2-book.org/introduction)</sup> 前身包 ggplot 在 useR! 2006 上展示，结合了 base 和 lattice 图形的优势，自动处理条件化和共享坐标轴，并能从多个数据源逐步构建图形；它由四个构建块组成：美学映射函数、图形对象（grob）函数、标度对象和图形对象<sup>[10](https://www.r-project.org/conferences/useR-2006/Abstracts/Wickham.pdf)</sup>。

与 lattice 一样，ggplot2 使用保罗·默雷尔（Paul Murrell）开发的 grid 包绘图，该包允许在图形对象（grob）创建后对其进行修改；相比之下，base 图形使用纸笔模型，没有用户可访问的成品图形表示<sup>[4](https://ggplot2-book.org/introduction)</sup>。迪帕扬·萨卡尔（Deepayan Sarkar）的 lattice 实现了威廉·克利夫兰（William Cleveland）的格状系统<sup>[4](https://ggplot2-book.org/introduction)</sup>。

该包的思想发表于 2010 年的分层语法论文和 2011 年《WIREs 计算统计》上的一篇文章，后者阐述了该理论对创建新图形的意义、可编程图形的重要性，以及围绕该包成长起来的生态系统<sup>[3](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)</sup><sup> • </sup><sup>[11](https://wires.onlinelibrary.wiley.com/doi/10.1002/wics.147)</sup>。配套书籍《ggplot2：数据分析的优雅图形》现已出到第三版，在线发布于 ggplot2-book.org<sup>[4](https://ggplot2-book.org/introduction)</sup>。

## 数据一览

采用规模庞大且可量化。R 观测台仪表盘记录每年约 2770 万次 CRAN 下载，大约每天 7.6 万次、每月 230 万次，在 CRAN 包中排名第 5<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup>。它作为基础设施的角色体现在依赖图中：4955 个包直接依赖它（393 个硬 Depends，4562 个 Imports，另有 1961 个间接关系），2135 个包建议使用它<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup>。

对于如此核心的包来说，安装足迹不大：安装 ggplot2 会额外引入 16 个包，其中 7 个含编译代码，源码压缩包共 7.6 MB，最重的是 isoband（1.5 MB）、farver（1.2 MB）和 viridisLite（1.2 MB）<sup>[2](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)</sup>。它要求 R 4.1 或更高版本，并导入 cli、gtable（>= 0.3.6）、isoband、rlang（>= 1.1.0）、S7、scales（>= 1.4.0）、vctrs（>= 0.6.0）和 withr（>= 2.5.0）等<sup>[6](https://cran.r-project.org/web/packages/ggplot2/refman/ggplot2.html)</sup>。

在速度方面，一位实践者的比较将 ggplot2 评为相对于 base 图形和 lattice 较慢，后两者被评为快；这一代价换来的是基于语法的模型、多种图例类型、良好的直接标签和扩展能力<sup>[12](https://rcdata.nau.edu/genomic-ml/public_html/graphics.html)</sup>。

## 与其他图形系统的比较

R 的三大图形系统体现了不同的模型。base 图形是命令式的、纸笔式的，图形一旦绘制完成就没有用户可访问的表示；它速度快、文档完善，但一位实践者的比较指出，它在带共享坐标轴的多面板显示方面很笨拙，需要手动定义坐标轴标度，并将其图例和标签评为较差<sup>[4](https://ggplot2-book.org/introduction)</sup><sup> • </sup><sup>[12](https://rcdata.nau.edu/genomic-ml/public_html/graphics.html)</sup>。lattice 构建在 grid 之上，实现格状显示，自动处理坐标轴标度和标注，图例良好<sup>[4](https://ggplot2-book.org/introduction)</sup><sup> • </sup><sup>[12](https://rcdata.nau.edu/genomic-ml/public_html/graphics.html)</sup>。ggplot2 同样基于 grid，构建在声明式、基于语法的模型之上<sup>[12](https://rcdata.nau.edu/genomic-ml/public_html/graphics.html)</sup>。

在交互性方面，plotly 的 R 接口提供了 `ggplotly()` 函数，可将许多 ggplot2 图形转换为对应的交互式版本，将静态语法桥接到一个 JavaScript 可视化工具包<sup>[4](https://ggplot2-book.org/introduction)</sup>。

## 生态系统与扩展

该包面向对象的核心是 ggproto 系统，开发者通过子类化它来创建新的几何对象、统计变换、标度，以及自 3.5.0 版以来的导引组件<sup>[13](https://tidyverse.org/blog/2024/02/ggplot2-3-5-0/)</sup>。社区维护的扩展展示集托管在 exts.ggplot2.tidyverse.org<sup>[1](https://ggplot2.tidyverse.org/)</sup>。

## 近期发展

**3.4.0 版**（2022-11-04 在 CRAN 发布）增加了 `linewidth` 美学，从 `size` 手中接管了任何线条宽度的尺寸设定；该版本还用 cli 包重做了错误消息，并在内部采用了 vctrs<sup>[7](https://ggplot2.tidyverse.org/news/)</sup>。

**3.5.0 版**（2024 年 2 月）重写了导引系统，即坐标轴和图例，改用 ggproto，使导引组件在多年作为 ggplot2 唯一仍基于纯 S3 的主要子系统之后，成为开发者的正式扩展点<sup>[13](https://tidyverse.org/blog/2024/02/ggplot2-3-5-0/)</sup>。它引入了 `coord_radial()`，作为 `coord_polar()` 的后继者，具有更多自定义能力：它通过 `guide_axis_theta()` 与新的导引系统集成，可以通过 `end` 参数绘制圆扇形，并避免数据在图形中心消失<sup>[13](https://tidyverse.org/blog/2024/02/ggplot2-3-5-0/)</sup><sup> • </sup><sup>[14](https://github.com/tidyverse/ggplot2/releases/tag/v3.5.0)</sup>。它还重新定义了 `I()` 的用途，使 AsIs 变量被标度系统忽略，而不是被赋予恒等标度<sup>[13](https://tidyverse.org/blog/2024/02/ggplot2-3-5-0/)</sup>。

**4.0.0 版**（2025-09-11 在 CRAN 发布）是一次主要版本发布，标志着该包诞生 18 周年，用 S7 对象系统替换了 ggplot2 的 S3 部分<sup>[7](https://ggplot2.tidyverse.org/news/)</sup><sup> • </sup><sup>[8](https://tidyverse.org/blog/2025/09/ggplot2-4-0-0/)</sup>。它引入了 `gg_par()`，这是 `grid::gpar()` 的包装器，应用 ggplot2 对设置的解读并将其转换为 grid 设置；例如，`linewidth` 在 ggplot2 中以毫米为单位解读，而 grid 期望的是点，该包装器还能防止线型和描边参数为 NA 所引发的问题<sup>[8](https://tidyverse.org/blog/2025/09/ggplot2-4-0-0/)</sup>。几何对象的默认值现在可以在主题中全局设置，`geom_violin()` 的分位数参数取代了 `draw_quantiles` 并移至 `stat_ydensity()`，许多几何对象中的填充美学通过 `fill_alpha()` 接受 grid 图案和渐变<sup>[7](https://ggplot2.tidyverse.org/news/)</sup>。当前的 CRAN 参考手册将 4.0.3 版描述为基于《图形语法》创建图形的声明式系统<sup>[15](https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf)</sup>。

## 用户

维护者表示，ggplot2 已有超过 10 年历史，数十万人用它绘制了数百万张图，变更通常仅限于添加新函数或参数，以便现有代码继续工作<sup>[5](https://github.com/tidyverse/ggplot2/)</sup>。2011 年《WIREs》文章的一份早期快照记录，截至 2010 年 11 月，邮件列表有近 1200 名成员和约 7000 条消息，用户来自智库、非营利组织（包括世界卫生组织）、咨询公司、政府机构、联邦实验室，以及 eBay、Google 和 Facebook 等公司<sup>[16](https://vita.had.co.nz/papers/ggplot2-wires.pdf)</sup>。

## References

1. [Create Elegant Data Visualisations Using the Grammar of Graphics • ggplot2 (official package site)](https://ggplot2.tidyverse.org/)
2. [Metrics for ggplot2, R Observatory](https://r-observatory.thecoatlessprofessor.com/packages/ggplot2)
3. [Hadley Wickham (2010). A Layered Grammar of Graphics. Journal of Computational and Graphical Statistics (author's preprint)](https://stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf)
4. [Introduction – ggplot2: Elegant Graphics for Data Analysis (3e)](https://ggplot2-book.org/introduction)
5. [tidyverse/ggplot2 (GitHub repository)](https://github.com/tidyverse/ggplot2/)
6. [Help for package ggplot2 (CRAN reference manual)](https://cran.r-project.org/web/packages/ggplot2/refman/ggplot2.html)
7. [Changelog • ggplot2](https://ggplot2.tidyverse.org/news/)
8. [ggplot2 4.0.0 (tidyverse blog)](https://tidyverse.org/blog/2025/09/ggplot2-4-0-0/)
9. [Internals of ggplot2 – ggplot2: Elegant Graphics for Data Analysis (3e)](https://ggplot2-book.org/internals.html)
10. [Hadley Wickham (2006). An implementation of the grammar of graphics in R: ggplot (useR! 2006 abstract)](https://www.r-project.org/conferences/useR-2006/Abstracts/Wickham.pdf)
11. [Hadley Wickham (2011). ggplot2. WIREs Computational Statistics 3: 180–185](https://wires.onlinelibrary.wiley.com/doi/10.1002/wics.147)
12. [Comparison of R graphics subsystems (NAU course notes)](https://rcdata.nau.edu/genomic-ml/public_html/graphics.html)
13. [ggplot2 3.5.0 (tidyverse blog)](https://tidyverse.org/blog/2024/02/ggplot2-3-5-0/)
14. [ggplot2 3.5.0 release notes (GitHub)](https://github.com/tidyverse/ggplot2/releases/tag/v3.5.0)
15. [ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics (CRAN reference manual, v4.0.3)](https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf)
16. [Hadley Wickham (2011). ggplot2 (author's copy, WIREs Computational Statistics)](https://vita.had.co.nz/papers/ggplot2-wires.pdf)

---
*Initially written Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Applied, official, and domain statistics › Computational statistics › Statistical graphics and computational data visualization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/ggplot2

English original: https://www.edgepedia.org/ggplot2
