前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2绘制嵌套圆图

ggplot2绘制嵌套圆图

作者头像
R语言数据分析指南
发布2022-12-20 20:05:27
5280
发布2022-12-20 20:05:27
举报
文章被收录于专栏:R语言数据分析指南

❝本节来介绍如何使用「ggplot2」来绘制嵌套圆形图,图形绘制倒也简单主要是细节的调整结果仅供参考❞

加载R包

代码语言:javascript
复制
library(tidyverse)

导入数据

代码语言:javascript
复制
bytes_total <- read_csv("bytes_total.txt")
speed_index <- read_csv("speed_index.txt")

数据清洗

代码语言:javascript
复制
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))

数据可视化

代码语言:javascript
复制
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")
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 导入数据
  • 数据清洗
  • 数据可视化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档