❝本节来介绍一个R包「ggtricks」,其主要来绘制一系列圆形图,通过此包可以在绘制不同风格的圆圈图来展示数据,作者给出了很多丰富的精彩案例,更多具体内容请参考官方文档。 ❞
❝https://abdoulma.github.io/ggtricks/ ❞
❝geom_pie 饼状图 ,geom_donut 甜甜圈图(带洞的饼图) geom_slice 部分饼图,geom_donut_slice 甜甜圈图的一部分 ❞
install.packages("ggtricks")
library(tidyverse)
library(ggtricks)
my_theme <- function(...) {
theme_minimal() +
theme(
text = element_text(family = "Atkinson Hyperlegible"),
axis.text.y = element_text(color = "black", size = rel(1.5))
)
}
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()
prod_df %>%
ggplot() +
geom_series_circles(aes(index, good, fill = good),linewidth = 2.5) +
coord_equal() +
my_theme()
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()
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()
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))
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()
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()
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()
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()