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 StartedRust098- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
MiMo-V2.5-ProMiMo-V2.5-Pro作为旗舰模型,擅⻓处理复杂Agent任务,单次任务可完成近千次⼯具调⽤与⼗余轮上 下⽂压缩。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
Kimi-K2.6Kimi K2.6 是一款开源的原生多模态智能体模型,在长程编码、编码驱动设计、主动自主执行以及群体任务编排等实用能力方面实现了显著提升。Python00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
项目优选
收起
deepin linux kernel
C
28
16
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
567
98
暂无描述
Dockerfile
708
4.51 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
958
955
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.61 K
942
Ascend Extension for PyTorch
Python
572
694
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
413
339
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.42 K
116
暂无简介
Dart
951
235
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
2