首页
/ RStudio Conf 2022 ggplot2 图形设计:极坐标太空任务可视化解析

RStudio Conf 2022 ggplot2 图形设计:极坐标太空任务可视化解析

2025-06-02 05:13:01作者:卓炯娓

项目背景

本文基于 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())

数据处理步骤包括:

  1. 按探索人员姓名分组
  2. 计算每位探索人员的总太空停留时间
  3. 记录首次和最后一次任务年份
  4. 对年份取负值以便后续可视化
  5. 为每位探索人员分配唯一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))

关键几何对象:

  1. geom_linerange() - 创建从基线到数据点的垂直线段
  2. geom_point() - 在基线位置添加灰色方块标记
  3. 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))

比例尺设置包括:

  1. x轴和y轴的范围和扩展
  2. 颜色渐变使用YlGnBu调色板
  3. 气泡大小范围
  4. 透明度范围

主题与标签优化

主题设置

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")

扩展功能包括:

  1. ggforce::geom_link() - 创建平滑曲线而非直线段
  2. ggblur::geom_point_blur() - 添加模糊效果的点
  3. scico::scale_color_scico() - 使用科学配色方案

设计要点总结

  1. 极坐标转换:将传统条形图转换为极坐标形式,增强视觉冲击力
  2. 层次设计:通过线条、基点和数据点的组合构建完整图表
  3. 视觉编码:使用颜色、大小和透明度三重编码表示数据值
  4. 标签优化:精心设计标签位置和样式确保可读性
  5. 主题定制:完全自定义主题元素,创造独特的视觉风格
  6. 扩展应用:利用扩展包实现更丰富的视觉效果

这种可视化方法不仅适用于太空任务数据,也可应用于其他时间序列和累积数据的展示,如项目进度、运动数据等。关键在于理解ggplot2的分层语法和极坐标转换原理,再结合创意设计思维。

登录后查看全文
热门项目推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
260
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
854
505
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
254
295
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
331
1.08 K
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
397
370
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
kernelkernel
deepin linux kernel
C
21
5