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 StartedRust0152- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
LongCat-Video-Avatar-1.5最新开源LongCat-Video-Avatar 1.5 版本,这是一款经过升级的开源框架,专注于音频驱动人物视频生成的极致实证优化与生产级就绪能力。该版本在 LongCat-Video 基础模型之上构建,可生成高度稳定的商用级虚拟人视频,支持音频-文本转视频(AT2V)、音频-文本-图像转视频(ATI2V)以及视频续播等原生任务,并能无缝兼容单流与多流音频输入。00
auto-devAutoDev 是一个 AI 驱动的辅助编程插件。AutoDev 支持一键生成测试、代码、提交信息等,还能够与您的需求管理系统(例如Jira、Trello、Github Issue 等)直接对接。 在IDE 中,您只需简单点击,AutoDev 会根据您的需求自动为您生成代码。Kotlin03
Intern-S2-PreviewIntern-S2-Preview,这是一款高效的350亿参数科学多模态基础模型。除了常规的参数与数据规模扩展外,Intern-S2-Preview探索了任务扩展:通过提升科学任务的难度、多样性与覆盖范围,进一步释放模型能力。Python00
skillhubopenJiuwen 生态的 Skill 托管与分发开源方案,支持自建与可选 ClawHub 兼容。Python0112
热门内容推荐
最新内容推荐
项目优选
收起
暂无描述
Dockerfile
733
4.75 K
Ascend Extension for PyTorch
Python
647
795
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
434
395
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.01 K
1.01 K
Claude 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 Started
Rust
1.18 K
152
deepin linux kernel
C
30
16
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
146
237
暂无简介
Dart
984
252
昇腾LLM分布式训练框架
Python
166
198
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.68 K
989