前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2优雅的拆分堆砌条形图

ggplot2优雅的拆分堆砌条形图

作者头像
R语言数据分析指南
发布2022-09-21 15:26:08
4400
发布2022-09-21 15:26:08
举报

❝本节来介绍如何「对堆砌条形图来进行图形拆分」

加载R包

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

定义主题

代码语言:javascript
复制
theme_niwot <- function(){
  theme_minimal()+
    theme(axis.text = element_text(color = "black",size = 6),
          strip.text = element_text(color = "black",hjust = 0, 
                                    margin = margin(l = -0.5, b = 5)),
          legend.key.size = unit(0.4, 'cm'),
          legend.text = element_text(color = "black",size =6),
          legend.margin = margin(),
          legend.position = 'top',
          plot.title.position = 'plot',
          panel.grid.minor = element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.major = element_line(size = 0.5,linetype = 3, color = "black"))
}

定义调色板

代码语言:javascript
复制
color_palette <- viridisLite::mako(6)[-c(1, 6)]

数据清洗

代码语言:javascript
复制
mpg_2008 <- mpg %>% 
  filter(year == 2008,!(class %in% c('2seater', 'minivan'))) %>% 
  mutate(class = case_when(
      class %in% c('compact', 'subcompact') ~ '(Sub-)Compact',
      class %in% c('pickup', 'suv') ~ 'Pickup/SUV',
      T ~ str_to_title(class)),
    manufacturer = str_to_title(manufacturer),
    manufacturer = fct_infreq(manufacturer) %>% fct_rev())

绘制主图

代码语言:javascript
复制
unsplit_plot <- mpg_2008 %>% 
  ggplot(aes(y = manufacturer, fill = class)) +
  geom_bar(position = position_stack(reverse = T)) +
  scale_fill_manual(values = color_palette) +
  scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
  scale_y_discrete(expand = expansion(mult = 0)) +
  geom_vline(xintercept = 0, size = 1) +
  theme_niwot()+
  labs(x = element_blank(),y=element_blank(),fill = element_blank())

拆分条形图

代码语言:javascript
复制
class_plots <- mpg_2008 %>% 
  ggplot(aes(y = manufacturer, fill = class)) +
  geom_bar() +
  scale_fill_manual(values =color_palette) +
  facet_wrap(vars(class)) +
  scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
  scale_y_discrete(expand = expansion(mult = 0)) +
  geom_vline(xintercept = 0, size = 1) +
  labs(x = element_blank(),y=element_blank(),fill = element_blank())+
  theme_niwot()

绘制总图

代码语言:javascript
复制
total_plot <- mpg_2008 %>% 
  ggplot(aes(y = manufacturer)) +
  geom_bar(fill = color_palette[4]) +
  scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
  scale_y_discrete(expand = expansion(mult = 0)) +
  geom_vline(xintercept = 0, size = 1) +
  facet_wrap(vars('Total')) +
  labs(x = element_blank(),y=element_blank(),fill = element_blank())+
  theme_niwot()

拼图

代码语言:javascript
复制
split_plot <- wrap_elements(plot = class_plots + total_plot +
    plot_layout(widths = c(0.75, 0.25)))
代码语言:javascript
复制
unsplit_plot / plot_spacer() / split_plot + 
  plot_layout(heights = c(0.425, 0.01,  0.575))

❝本节介绍到此结束细节还是蛮多的,但是通过此文你一定有所收获

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 定义主题
  • 定义调色板
  • 数据清洗
  • 绘制主图
  • 拆分条形图
  • 绘制总图
  • 拼图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档