前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggtricks丰富多彩绘圈图

ggtricks丰富多彩绘圈图

作者头像
R语言数据分析指南
发布2023-08-18 13:27:59
1220
发布2023-08-18 13:27:59
举报

欢迎关注R语言数据分析指南

❝本节来介绍一个R包「ggtricks」,其主要来绘制一系列圆形图,通过此包可以在绘制不同风格的圆圈图来展示数据,作者给出了很多丰富的精彩案例,更多具体内容请参考官方文档。 ❞

官方文档

❝https://abdoulma.github.io/ggtricks/ ❞

ggtricks主要函数

❝geom_pie 饼状图 ,geom_donut 甜甜圈图(带洞的饼图) geom_slice 部分饼图,geom_donut_slice 甜甜圈图的一部分 ❞

加载R包

代码语言:javascript
复制
install.packages("ggtricks")
library(tidyverse)
library(ggtricks)

定义主题

代码语言:javascript
复制
my_theme <- function(...) {
  theme_minimal() +
    theme(
      text = element_text(family = "Atkinson Hyperlegible"),
      axis.text.y = element_text(color = "black", size = rel(1.5))
    )
}

案例一

代码语言:javascript
复制
prod_df <- data.frame(
  good = c("Potatoes", "Sugar", "Butter", "Coffee", "Rice", "Eggs", "Flour", "Tea", "Milk"),
  index = c(606, 485, 204, 165, 215, 268, 267, 137, 194)
)

prod_df <- prod_df |>
  mutate(
    index = index / 100,
    good = fct_rev(fct_inorder(good))
  )

prod_df |>
  ggplot() +
  geom_series_circles(aes(index, good), color = "white") +
  coord_equal() +
  my_theme()
添加填充颜色
代码语言:javascript
复制
prod_df %>% 
  ggplot() +
  geom_series_circles(aes(index, good, fill = good),linewidth = 2.5) +
  coord_equal() +
  my_theme()
添加标签
代码语言:javascript
复制
prod_df %>% 
  ggplot() +
  geom_series_circles(aes(index, good, fill = good), color = "black", linewidth = 2.5) +
  geom_series_text(aes(index, good, label = index), size =4, family = "Atkinson Hyperlegible") +
  coord_equal(clip = "off") +
  guides(fill = "none") +
  my_theme()

定义不同起始角度

代码语言:javascript
复制
index_df <- tribble(
  ~article, ~index,
  "Plate beef", 187,
  "Bacon", 215,
  "Lard", 266
)

index_df <- index_df |>
  mutate(index = index / 100)

index_df |> ggplot() +
  geom_series_circles(aes(index, article),
    init_angle = 45) +
  coord_equal() +
  theme_minimal()
代码语言:javascript
复制
usa_trades <- tribble(
  ~country, ~with_foreign, ~with_us,
  "United States", 13359, 13359,
  "United Kingdom", 15925, 3123,
  "Canada", 2304, 1256,
  "France", 7429, 1686,
  "Italy", 4189, 1516,
  "Netherlands", 2639, 316,
  "Japan", 2421, 1420,
  "Germany", 4966, 577
)

usa_trades <- usa_trades |>
  mutate(
    country = fct_rev(fct_inorder(country)),
    across(.cols = contains("with"), \(x) x / 1e3, .names = "{.col}")
  ) |>
  arrange(country) |>
  mutate(row_num = row_number())

n_rows <- nrow(usa_trades)
usa_trades |>
  ggplot() +
  geom_series_circles(aes(with_foreign, country), fill = "white", color = "black", linewidth = 2) +
  geom_series_circles(aes(with_us, country)) +
  geom_text(aes(y = row_num, label = scales::comma(with_us)), x = -1, family = "Atkinson Hyperlegible", hjust = 1) +
  geom_text(aes(y = row_num, label = scales::comma(with_foreign)), x = -2, family = "Atkinson Hyperlegible", hjust = 1) +
  geom_text(aes(y = row_num, label = country), x = -3.5, family = "Atkinson Hyperlegible", fontface = "bold", hjust = 1) +
  annotate(geom = "text", x = -1, y = n_rows + 1, label = "Trade\n with\n U.S.", family = "Atkinson Hyperlegible", hjust = 1) +
  annotate(geom = "text", x = -2, y = n_rows + 1, label = "Total\n Foreign\n Trade", family = "Atkinson Hyperlegible", hjust = 1) +
  annotate(geom = "text", x = 8, y = n_rows + 1, label = "(Millions of Dollars)", family = "Atkinson Hyperlegible") +
  scale_x_continuous(limits = c(-5, 14)) +
  coord_equal(clip = "off") +
  theme_minimal() +
  theme(axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(), 
    plot.background = element_rect(fill = "white", color = NA))

绘制甜甜圈图

代码语言:javascript
复制
my_df <- data.frame(cat = paste0("Prod ", 1:4),val = c(87, 34, 21, 8))

categories_fills <- c("Prod 1" = "#3E71EC","Prod 2" = "#A9A9A9",
  "Prod 3" = "#7942A6","Prod 4" = "#F7324B")
  
  my_df |> ggplot() +
  geom_donut(aes(cat = cat, val = val, fill = cat),
    r1 = 1, r2 = .65) +
  coord_equal() +
  scale_fill_manual(values = categories_fills,guide = "none") +
  theme_minimal()
设置角度
代码语言:javascript
复制
my_df |>
  ggplot() +
  geom_slice(aes(cat = cat, val = val, fill = cat),slice_angle = 180) +
  coord_equal() +
  scale_fill_manual(values = categories_fills,guide = "none") +
  theme_minimal()
代码语言:javascript
复制
my_df |> ggplot() +
  geom_donut_slice(aes(cat = cat, val = val, fill = cat),r1 = 1, r2 = .65) +
  coord_equal() +
  scale_fill_manual(values = categories_fills,guide = "none") +
  theme_minimal()

添加连接线

代码语言:javascript
复制
my_df |>
  ggplot() +
  geom_donut_slice(aes(cat = cat, val = val, fill = cat),
    r1 = 1, r2 = .65,slice_angle = 120,
    slice_position = "top",
    link_with_origin = TRUE) +
  coord_equal(clip = "off") +
  scale_fill_manual(values = categories_fills,guide = "none") +
  theme_minimal()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 欢迎关注R语言数据分析指南
  • 官方文档
  • ggtricks主要函数
  • 加载R包
  • 定义主题
  • 案例一
    • 添加填充颜色
      • 添加标签
      • 定义不同起始角度
      • 绘制甜甜圈图
        • 设置角度
        • 添加连接线
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档