使用ggplot2和ggtext创建精美的企鹅数据集可视化图表
2025-06-02 04:05:58作者:申梦珏Efrain
本文基于rstudio-conf-2022中关于ggplot2图形设计的研讨会材料,重点讲解如何使用ggplot2和ggtext包创建带有精美注释的企鹅数据集可视化图表。
数据准备
首先我们需要加载并了解数据集。我们使用的是palmerpenguins包提供的企鹅数据集:
library(palmerpenguins)
penguins
这个数据集包含了三种企鹅(阿德利企鹅、帽带企鹅和巴布亚企鹅)的体型测量数据,包括喙长、喙深、鳍状肢长度和体重等信息。
基础图表构建
我们从最基本的散点图开始,展示企鹅喙长和喙深的关系:
ggplot(
penguins,
aes(x = bill_length_mm, y = bill_depth_mm,
color = species, size = body_mass_g)
) +
geom_point(alpha = .2) +
labs(
x = "Bill length (mm)",
y = "Bill depth (mm)",
title = "Bill dimensions of brush-tailed penguins Pygoscelis spec.",
caption = "Horst AM, Hill AP, Gorman KB (2020). palmerpenguins R package version 0.1.0"
)
图表美化步骤
1. 添加点轮廓
为了让数据点更清晰可见,我们添加白色轮廓:
geom_point(alpha = .2, stroke = .3) +
geom_point(shape = 1, stroke = .3)
2. 自定义颜色和大小比例尺
scale_color_manual(
name = "Species:",
values = c("#FF8C00", "#A034F0", "#159090")
) +
scale_size(
name = "Body mass:",
breaks = 3:6 * 1000,
labels = function(x) paste(x / 1000, "kg"),
range = c(.5, 5)
)
3. 调整坐标轴
coord_cartesian(
expand = FALSE,
clip = "off"
) +
scale_x_continuous(
limits = c(30, 60),
breaks = 6:12*5
) +
scale_y_continuous(
limits = c(12.5, 22.5),
breaks = seq(12.5, 22.5, by = 2.5)
使用ggtext增强文本样式
ggtext包允许我们在图表中使用Markdown和HTML标记来格式化文本:
library(ggtext)
labs(
x = "Bill length *(mm)*",
y = "Bill depth *(mm)*",
title = "Bill dimensions of brush-tailed penguins *Pygoscelis spec.*",
caption = "Horst AM, Hill AP, Gorman KB (2020). <span style='font-family:tabular;'>palmerpenguins</span> R package version 0.1.0"
) +
theme(
plot.title = element_markdown(
face = "bold", size = 16, margin = margin(12, 0, 12, 0)
)
添加物种标签注释
我们需要先创建一个包含物种平均数据和格式化标签的数据框:
penguins_labs <-
penguins %>%
group_by(species) %>%
summarize(across(starts_with("bill"), ~ mean(.x, na.rm = TRUE))) %>%
mutate(
species_lab = case_when(
species == "Adelie" ~ "<b style='font-size:15pt;'>*P. adéliae*</b><br>(Adélie penguin)",
species == "Chinstrap" ~ "<b style='font-size:15pt;'>*P. antarctica*</b><br>(Chinstrap penguin)",
species == "Gentoo" ~ "<b style='font-size:15pt;'>*P. papua*</b><br>(Gentoo penguin)"
)
)
然后使用geom_richtext添加注释:
geom_richtext(
data = penguins_labs,
aes(label = species_lab,
color = species,
color = after_scale(colorspace::darken(color, .4))),
family = "Roboto Condensed",
size = 3, lineheight = .8,
fill = "#ffffffab", ## 半透明白色背景
show.legend = FALSE
)
最终主题定制
theme_minimal(
base_size = 10, base_family = "Roboto Condensed"
) +
theme(
plot.title.position = "plot",
plot.caption.position = "plot",
axis.text = element_text(family = "Tabular"),
legend.text = element_text(color = "grey50"),
plot.margin = margin(0, 14, 0, 12),
plot.background = element_rect(fill = NA, color = "grey50", size = 1)
)
总结
通过本教程,我们学习了如何:
- 使用ggplot2创建基础可视化
- 通过多种几何对象增强图表表现力
- 自定义比例尺和坐标轴
- 使用ggtext包实现丰富的文本格式化
- 添加和样式化注释
- 全面定制图表主题
这些技巧可以应用于各种数据可视化场景,帮助你创建既美观又专业的图表。
登录后查看全文
热门项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
Baichuan-M3-235BBaichuan-M3 是百川智能推出的新一代医疗增强型大型语言模型,是继 Baichuan-M2 之后的又一重要里程碑。Python00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
热门内容推荐
项目优选
收起
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
539
3.76 K
Ascend Extension for PyTorch
Python
345
412
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
888
605
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
337
182
暂无简介
Dart
777
192
deepin linux kernel
C
27
11
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
758
React Native鸿蒙化仓库
JavaScript
303
356
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
987
252
仓颉编译器源码及 cjdb 调试工具。
C++
154
896