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的分层语法和极坐标转换原理,再结合创意设计思维。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0194- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
602
4.04 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
Ascend Extension for PyTorch
Python
442
531
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
112
170
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.46 K
825
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
922
770
暂无简介
Dart
847
204
React Native鸿蒙化仓库
JavaScript
321
375
openGauss kernel ~ openGauss is an open source relational database management system
C++
174
249