分享

图上嵌图!

 阿越就是我 2023-10-12 发布于上海

前段时间科室有个数据需要汇总一下,做个PPT给领导看,主要就是业务占比和增长情况,其实就是简单的条形图和折线图就能搞定。今天发现这个数据还可以使用图上加图的方式实现一下。

使用的y叔的R包ggimage中的geom_subview函数。使用起来也是非常简单,完美的图层语法,就是简单的图层叠加。

加载R包和数据

library(tidyverse)
## -- Attaching packages ----------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## -- Conflicts -------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggimage)
library(gtable)

# 数据可以自己构造
df2 <- read.csv('../000files/df2.csv',header = T)
str(df2)
## 'data.frame': 12 obs. of  3 variables:
##  $ 年份 : int  2017 2017 2017 2018 2018 2018 2019 2019 2019 2020 ...
##  $ 科室 : chr  "2型糖尿病" "糖尿病肾病" "糖尿病周围神经病变" "2型糖尿病" ...
##  $ value: int  72 14 10 178 11 32 167 22 44 249 ...

数据是每个年份不同业务(糖尿病肾病、周围神经病变、2型糖尿病)的数量。

首先是把4个饼图画出来。

p21 <- ggplot(df2[1:3,],aes(x='',y=value,fill=科室))+
  geom_bar(stat = 'identity',width = 1,position = 'stack')+
  scale_y_continuous(expand = c(0,0))+
  coord_polar(theta="y") +
    theme_void() + theme(legend.position="none"

p22 <- ggplot(df2[4:6,],aes(x='',y=value,fill=科室))+
  geom_bar(stat = 'identity',width = 1,position = 'stack')+
  scale_y_continuous(expand = c(0,0))+
  coord_polar(theta="y") +
    theme_void() + theme(legend.position="none"

p23 <- ggplot(df2[7:9,],aes(x='',y=value,fill=科室))+
  geom_bar(stat = 'identity',width = 1,position = 'stack')+
  scale_y_continuous(expand = c(0,0))+
  coord_polar(theta="y") +
    theme_void() + theme(legend.position="none"

p24 <- ggplot(df2[10:12,],aes(x='',y=value,fill=科室))+
  geom_bar(stat = 'identity',width = 1,position = 'stack')+
  scale_y_continuous(expand = c(0,0))+
  coord_polar(theta="y") +
    theme_void() + theme(legend.position="none"

然后画出基本的画板,在把饼图叠加上去即可!

# 构建一个数据框
base_df <- df2 %>% 
  group_by(年份) %>% 
  summarise(n = sum(value)) %>% 
  mutate(width = n / sum(n) * 400# 根据比例调整饼图大小
         pie = list(p21,p22,p23,p24) # tibble可以存放列表
         )

base_df
## # A tibble: 4 x 4
##    年份     n width pie   
##   <int> <int> <dbl> <list>
## 1  2017    96  44.0 <gg>  
## 2  2018   221 101.  <gg>  
## 3  2019   233 107.  <gg>  
## 4  2020   323 148.  <gg>

画图代码非常简单:

base <- ggplot(data=base_df, aes(年份, n))+geom_blank()+
  scale_x_continuous(limits = c(2017,2020.8))+
  scale_y_continuous(limits = c(70,400),name = NULL)+theme_minimal()

base + geom_subview(aes(x=年份, y=n, 
                        subview=pie,width=width, height=width), data=base_df)
plot of chunk unnamed-chunk-4

看起来还是挺美观的,数量和比例也是一目了然,在一个图中呈现汇总信息和单个年份的比例。

最后加上图例(这段神奇的代码也是来自于y叔):

leg1 <- gtable_filter(
    ggplot_gtable(
        ggplot_build(p21 + theme(legend.position="right"))
    ), "guide-box")

base + geom_subview(aes(x=年份, y=n, 
                        subview=pie,width=width, height=width), data=base_df)+
  geom_subview(subview = leg1, x = 2020.4,y = 110)
plot of chunk unnamed-chunk-5

不过领导表示不直观,还是柱状图和折线图好!

以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约