RStudio Conf 2022 ggplot2 图形设计:极坐标太空任务可视化解析
2025-06-02 02:22:34作者:卓炯娓
项目背景
本文基于 RStudio Conf 2022 中关于 ggplot2 图形设计的研讨会材料,重点解析如何创建极坐标太空任务可视化图表。该图表展示了太空探索人员在太空中的累计停留时间,以及他们首次和最后一次执行太空任务的年份。
数据准备
首先需要准备太空任务数据,包括:
library(tidyverse)
# 读取太空探索数据
df_astro <- read_csv('space_explorers.csv')
# 数据处理
df_missions <- df_astro %>%
group_by(name) %>%
summarize(
hours = sum(hours_mission),
year = min(year_of_mission),
max_year = max(year_of_mission)
) %>%
ungroup() %>%
mutate(year = -year) %>%
arrange(year) %>%
mutate(id = row_number())
数据处理步骤包括:
- 按探索人员姓名分组
- 计算每位探索人员的总太空停留时间
- 记录首次和最后一次任务年份
- 对年份取负值以便后续可视化
- 为每位探索人员分配唯一ID
基础可视化构建
核心图层设计
g1 <- ggplot(df_missions, aes(x = id, y = hours, color = hours)) +
geom_linerange(aes(ymin = 0, ymax = hours, alpha = hours), size = .25) +
geom_point(aes(y = 0), shape = 15, size = .1, color = "#808080") +
geom_point(aes(y = hours, size = hours))
关键几何对象:
geom_linerange()- 创建从基线到数据点的垂直线段geom_point()- 在基线位置添加灰色方块标记geom_point()- 在数据点位置添加彩色气泡
极坐标转换
g1 <- g1 + coord_polar(theta = "y", start = 0, clip = "off")
使用coord_polar()将直角坐标系转换为极坐标系,参数theta = "y"表示使用y轴作为角度轴。
比例尺调整
g1 <- g1 +
scale_x_continuous(limits = c(-300, NA), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 23000), expand = c(0, 0)) +
scale_color_distiller(palette = "YlGnBu", direction = -1) +
scale_size(range = c(.001, 3)) +
scale_alpha(range = c(.33, .95))
比例尺设置包括:
- x轴和y轴的范围和扩展
- 颜色渐变使用YlGnBu调色板
- 气泡大小范围
- 透明度范围
主题与标签优化
主题设置
g1 <- g1 +
theme_void() +
theme(
plot.background = element_rect(fill = "black"),
plot.margin = margin(-70, -70, -70, -70),
legend.position = "none"
)
使用theme_void()移除所有默认主题元素,并设置黑色背景和负边距以最大化绘图区域。
添加标签
# 准备标签数据
df_labs <- df_missions %>%
filter(year %in% -c(1961, 197:201*10, 2019)) %>%
group_by(year) %>%
filter(id == min(id))
df_max <- df_missions %>%
arrange(-hours) %>%
slice(1) %>%
mutate(
first_name = str_remove(name, ".*, "),
last_name = str_remove(name, "(?<=),.*"),
label = paste("Between", abs(year), "and", max_year, ",\n",
first_name, last_name, "has spent\n",
format(hours, big.mark = ','), "hours in space.\nThat's roughly",
round(hours / 24, 0), "days!")
)
# 添加标签
g2 <- g1 +
geom_text(
data = df_labs, aes(y = 0, label = abs(year)),
family = "Lato", fontface = "bold", color = "#808080",
size = 4.5, hjust = 1.2
) +
geom_text(
data = df_max, aes(label = label)),
family = "Lato", size = 3.9, vjust = -.35
)
标题和说明文字
g2 <- g2 +
annotate(
geom = "text", x = -300, y = 0, label = "Travelling to\nOuter Space",
family = "Boska", fontface = "bold", lineheight = .9,
size = 20, color = "white", hjust = .57, vjust = .45, alpha = .25
) +
annotate(
geom = "text", x = -300, y = 0, label = "Travelling to\nOuter Space",
family = "Boska", fontface = "bold", lineheight = .85,
size = 20, color = "white", hjust = .55, vjust = .4
) +
labs(caption = "Cumulative time in outer space...") +
theme(
plot.caption = element_text(
family = "Lato",
size = 15, color = "#808080", hjust = .5,
margin = margin(-100, 0, 100, 0)
)
)
高级扩展技巧
使用扩展包增强视觉效果:
# 使用ggforce和ggblur扩展包
g_ext <- ggplot(df_missions, aes(x = id, y = hours, color = hours)) +
ggforce::geom_link(aes(xend = id, yend = 0, alpha = hours), size = .25, n = 300) +
ggblur::geom_point_blur(aes(size = hours, blur_size = hours), blur_steps = 25) +
scico::scale_color_scico(palette = "buda") +
ggblur::scale_blur_size_continuous(range = c(.5, 10), guide = "none")
扩展功能包括:
ggforce::geom_link()- 创建平滑曲线而非直线段ggblur::geom_point_blur()- 添加模糊效果的点scico::scale_color_scico()- 使用科学配色方案
设计要点总结
- 极坐标转换:将传统条形图转换为极坐标形式,增强视觉冲击力
- 层次设计:通过线条、基点和数据点的组合构建完整图表
- 视觉编码:使用颜色、大小和透明度三重编码表示数据值
- 标签优化:精心设计标签位置和样式确保可读性
- 主题定制:完全自定义主题元素,创造独特的视觉风格
- 扩展应用:利用扩展包实现更丰富的视觉效果
这种可视化方法不仅适用于太空任务数据,也可应用于其他时间序列和累积数据的展示,如项目进度、运动数据等。关键在于理解ggplot2的分层语法和极坐标转换原理,再结合创意设计思维。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0444
源启盛夏_AtomGit暑期开发者成长计划「源启盛夏」暑期校园开发者成长计划旨在激活校园开源力量,通过积分激励、认证扶持、资源倾斜等形式,引导高校组织和开发者完成「入驻 — 建项目 — 做贡献 — 获认证 — 得资源」的完整闭环。无论你是想带领社团入驻平台的组织者,还是希望用代码贡献证明自己的开发者,都能在这里找到属于你的成长路径。Markdown00
jiuwenswarmJiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0760
Hy3Hy3 是由腾讯混元团队研发的快慢思考融合的混合专家模型,总参数量 295B,激活参数 21B,MTP 层参数 3.8B。4 月底发布 Hy3 Preview 后,我们在 50 多个业务中获得了广泛的反馈,修复了各种体验问题,进一步提升了后训练的质量和规模。今天,我们发布 Hy3。它展现出显著强于同尺寸并比肩旗舰(参数规模往往是 Hy3 的 2~5 倍)开源模型的智能水平,显著提升了在各类产品和生产力任务中的实用价值。Python00
AscendNPU-IRAscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优C++0310
DragonOSDragonOS is an operating system developed from scratch using Rust, with Linux compatibility. It is designed for **Serverless** scenarios. 使用Rust从0自研内核,具有Linux兼容性的操作系统,面向云计算Serverless场景而设计。Rust00
项目优选
收起
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
494
515
deepin linux kernel
C
32
16
Ascend Extension for PyTorch
Python
799
1.13 K
暂无描述
Markdown
825
5.48 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
780
1.57 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
964
2.27 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.2 K
1.24 K
CANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。
Jupyter Notebook
640
272
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
C
830
6.13 K
昇腾LLM分布式训练框架
Python
193
272