❝本节来介绍如何使用「ggplot2」来绘制嵌套圆形图,图形绘制倒也简单主要是细节的调整结果仅供参考❞
library(tidyverse)
bytes_total <- read_csv("bytes_total.txt")
speed_index <- read_csv("speed_index.txt")
mobile_bytes <- bytes_total %>%
filter(date %in% c("2022_10_01", "2018_10_01"), client == "mobile") %>%
select(measure, date, p50, p90) %>%
pivot_longer(c(p50, p90), names_to = "percentile") %>%
mutate(year = str_sub(date, 1, 4)) %>%
mutate(name = paste(percentile, year, sep = "_")) %>%
mutate(x = c(0.45, 1, -0.57, -1)) %>%
mutate(label = case_when(percentile == "p50" ~ paste0("Peso\nmediano:\n", round(value), " KB"),
percentile == "p90" ~ paste0("Percentil 90:\n", round(value), " KB"))) %>%
mutate(x_label = c(0.45, 1.4, -0.57, -1.3))
ggplot() +
geom_point(aes(x = x, size = value, color = year), y = 0, alpha = 0.5,
data = filter(mobile_bytes, percentile == "p90")) +
geom_point(aes(x = x, size = value, color = year), y = 0,
data = filter(mobile_bytes, percentile == "p50")) +
geom_text(aes(x = x_label, label = label), y = 0.4, size = 2.5,
data = filter(mobile_bytes, percentile == "p90")) +
geom_text(aes(x = x_label, label = label), y = 0, size = 2.5,
data = filter(mobile_bytes, percentile == "p50")) +
annotate("text", x = -1, y = 1.3, label = "Octubre 2018",size = 3.5) +
annotate("text", x = 1, y = 1.3, label = "Octubre 2022", size = 3.5) +
labs(x = NULL,y = NULL) +
scale_color_manual(values = c("#1b998b", "#fb6107")) +
scale_x_continuous(limits = c(-2, 2)) +
scale_y_continuous(limits = c(-1.0, 1.4)) +
scale_size_area(max_size = 82) +
coord_equal() +
theme(
plot.title = element_text(hjust = 0.5, size = 18),
plot.caption = element_text(hjust = 0.5,size = 7),
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position = "none")