zl程序教程

您现在的位置是:首页 >  Java

当前栏目

跟着nature学绘图之绘制组合版环饼图

2023-02-18 16:35:32 时间

本节来复现「nature medicine」上的一张图,此图将饼图与环状条形图进行了组合也别有一番风格,小编稍微写了一些代码有更高需求的可以在此基础上进行进一步的拓展;

原文

Data-driven identification of post-acute SARS-CoV-2 infection subphenotypes

加载R包

library(tidyverse)
library(readxl)
library(camcorder)
library(ggtext)
library(ggsci)
library(cowplot)

数据清洗

df <- read_excel("41591_2022_2116_MOESM5_ESM.xlsx") %>% 
  select(1,2,3,group_2) %>% 
  rownames_to_column(var="ID") %>% 
  mutate_at(vars(c(S1)),~str_split(.," ",simplify=T)[,1]) %>%
  mutate(group_3 = rep(LETTERS[1:4], times=c(5,12,10,11))) %>% 
  mutate(S1=as.numeric(S1),ID=as.numeric(ID)) %>% 
  group_by(group_2) %>% 
  mutate(per=S1/sum(S1)*10) %>% 
  arrange(desc(per)) %>% 
  ungroup()

绘制饼图

p <- df %>% 
  ggplot(aes(x="",y=per,fill=group_3))+
  geom_col()+ coord_polar("y")+
  scale_fill_npg()+
  theme_void()+
  theme(legend.position = "non")

绘制环状条图

p2 <- ggplot(df) +
# 设置刻度线
  annotate("segment", x = -Inf, xend = Inf, y = seq(0,10,1), yend = seq(0,10,1),
           size = rep(c(0.25, 0.1),length.out =11), alpha = 0.5)+
 # 添加标签 
  annotate("label", x = 0, y = seq(0,10,1), label = seq(0,10,1),color="black",
           size = 3, fill = "#FEF8FA",label.padding = unit(0.1,"lines"),
           label.size = 0)+
  geom_col(aes(x = ID, y =per, fill = group_2),position = "dodge", width=0.6)+
  scale_y_continuous(limits = c(-4,10)) +
  scale_x_continuous(limits = c(-0.5,38.5), breaks = 1:38.5) +
  scale_fill_manual(values=c("#709AE1FF","#8A9197FF","#D2AF81FF","#FD7446FF",
                    "#D5E4A2FF","#197EC0FF","#F05C3BFF","#46732EFF",
                    "#71D0F5FF","#075149FF","#C80813FF","#91331FFF"))+
  coord_polar()+
  theme_minimal() +
  theme(
    legend.position = "none",
    plot.background =element_blank(),
    panel.background = element_blank(),
    panel.grid = element_blank(),
    axis.text.y = element_blank(),
    axis.title = element_blank(),
    axis.text.x = element_blank())

拼图

ggdraw(p2)+
  draw_plot(p,scale=0.23,x=0,y=0)