前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gggibbous!好看的“月亮图”绘制,饼图平替,推荐...

gggibbous!好看的“月亮图”绘制,饼图平替,推荐...

作者头像
DataCharm
发布2024-01-04 11:51:07
1880
发布2024-01-04 11:51:07
举报
文章被收录于专栏:数据 学术 商业 新闻
  • 前言
    • 一、gggibbous-用月亮圆缺表示数据,有趣~~
    • 二、可视化学习圈子是干什么的?
    • 三、系统学习可视化
    • 四、猜你喜欢

gggibbous-用月亮圆缺表示数据,有趣~~

在整理我们的R语言可视化课程时,发现了一个非常有趣的图表类型-「月亮图(Moon charts)」 ,其用月亮的圆缺来表示占比数据的多少,是饼图替代图表类型。下面,我们就来介绍一下绘制月亮图的工具-「gggibbous」

gggibbous包介绍

「gggibbous」包就是绘制月亮图的基于ggplot2的R语言绘图工具,其提供的geom_moon() 函数就可以绘制月亮图。其安装脚本如下:

代码语言:javascript
复制
install.packages("gggibbous")
# or
devtools::install_github("mnbram/gggibbous")

月亮图和饼图的对比关系如下:

  • gggibbous包绘图案例
代码语言:javascript
复制
  library(gggibbous)
  ggplot(data.frame(x = 1:5, y = 1, size = 2^(0:4)), aes(x, y, size = size)) +
  geom_moon() +
  geom_point(y = 2) +
  lims(x = c(0.5, 5.5), y = c(0.5, 2.5)) +
  scale_size(range = c(5, 10))

基本样式

代码语言:javascript
复制
ggplot(data.frame(x = 1:5, y = 0, ratio = 0:4 * 0.25), aes(x = x, y = y)) +
  geom_moon(aes(ratio = ratio), size = 20, fill = "black") +
  geom_text(aes(y = y + 1, label = ratio)) +
  lims(x = c(0.5, 5.5), y = c(-1, 1.4)) +
  theme_void()

ratio aesthetic

代码语言:javascript
复制
tidymoons <- data.frame(
  x = rep(1:3, 6),
  y = rep(rep(3:1, each = 3), 2),
  ratio = c(1:9 / 10, 9:1 / 10),
  right = rep(c(TRUE, FALSE), each = 9)
)

ggplot(tidymoons) +
  geom_moon(aes(x, y, ratio = ratio, right = right, fill = right)) +
  lims(x = c(0.5, 3.5), y = c(0.5, 3.5))

right aesthetic

Legend key glyphs

代码语言:javascript
复制
dmeladh_adj <- dmeladh
dmeladh_adj$long <- dmeladh$Longitude + c(
  -2, 0, -2, 2, -3, 3, 3, 2, 3, 4, -2.5, -2.5, -1, -2, -2.5, -4, 2.5,
  5, 6, 7, 2, -7, -5.5, -3, 0, -7, -2, 3, 5.5, 0.5, -1, -1.5, -3, 2)
dmeladh_adj$lat <- dmeladh$Latitude + c(
  -2, 2, 0, 1, 0, 0, 0, 2, 0.5, -1, 1, -1.5, 2, 4, 1.5, 0, 2,
  1, -1, -3, -2, 1, -1, -2, -3, -2, -4, -3, -1, 1.5, 2, 2, -2, 0)

moonmap <- ggplot(dmeladh_adj, aes(long, lat)) +
  geom_polygon(
    data = map_data(
      "world", region = "(Australia)|(Indonesia)|(Papua New Guinea)"),
    aes(group = group),
    fill = "gray80"
  ) +
  geom_segment(aes(xend = Longitude, yend = Latitude), color = "gray20") +
  geom_point(aes(Longitude, Latitude), size = 0.75, color = "gray20") +
  scale_size(range = c(4, 10)) +
  coord_map(xlim = c(110, 160), ylim = c(-45, -5)) +
  theme_void() +
  theme(
    legend.position = c(0.05, 0.05),
    legend.direction = "horizontal",
    legend.justification = c(0, 0)
  )

moonmap +
  geom_moon(
    aes(ratio = AdhS / 100, size = N),
    right = FALSE, fill = "gold", color = "gold",
    key_glyph = draw_key_moon_left
  ) +
  geom_moon(
    aes(ratio = AdhF / 100, size = N),
    fill = "forestgreen", color = "forestgreen"
  )

地图样式

参考资料

[1]

gggibbous包官网: https://github.co/mnbram/gggibbous。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • gggibbous-用月亮圆缺表示数据,有趣~~
  • gggibbous包介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档