当前位置:   article > 正文

R语言 多组堆砌图_多样本堆砌图

多样本堆砌图

目录

数据格式

普通绘图

添加比例

R语言 堆砌图_r语言堆砌图-CSDN博客

关键点在于数据转换步骤和数据比例计算步骤,然后个性化调整图。

①data  <- melt(dat, id.vars = c("ID"))##根据分组变为长数据

②#计算百分比##
data2 <- ddply(data, 
            "ID",    ##需展示的X轴列名
            transform,
            percent = value / sum(value) * 100)#相当于按照样本分组,然后计算比例
#只提取需要的行
data2a <- data2[data2$variable=="high",]
data2a$percent <- round(data2a$percent,3)

数据格式
  1. rm(list = ls())
  2. library(ggplot2)
  3. library(tidyverse)
  4. library(reshape)
  5. library(plyr)
  6. library(patchwork)
  7. dat <- as.data.frame(cbind(1:5, 5:1))
  8. colnames(dat) <- c("high","low")
  9. dat$ID <- paste0("samp",1:nrow(dat))
head(dat)
  high low    ID
1    1   5 samp1
2    2   4 samp2
3    3   3 samp3
4    4   2 samp4
5    5   1 samp5

需要进行数据转换

  1. ##数据转换##
  2. data <- melt(dat, id.vars = c("ID"))##根据分组变为长数据


普通绘图
  1. p <- ggplot(data = data,
  2. aes(x=ID,y=value,fill=variable))+
  3. #geom_bar(stat = "identity",position = "stack")+ ##展示原来数值
  4. geom_bar(stat = "identity",position = "fill")+ ##按照比例展示:纵坐标为1
  5. scale_y_continuous(expand = expansion(mult=c(0.01,0.1)),##展示纵坐标百分比数值
  6. labels = scales::percent_format())+
  7. scale_fill_manual(values = c("high"="#98d09d","low"="#e77381"), ##颜色调整
  8. limits=c("high","low"))+ ##limit调整图例顺序
  9. theme(panel.background = element_blank(), ##主题设置
  10. axis.line = element_line(),
  11. legend.position = "bottom")+
  12. labs(title = "Title",x=NULL,y="percent")+ ##X,Y轴设置
  13. guides(fill=guide_legend(title = NULL,nrow = 1,byrow = FALSE))
  14. p
  15. #dev.off()


添加比例

计算百分比

  1. #计算百分比##
  2. data2 <- ddply(data,
  3. "ID", ##需展示的X轴列名
  4. transform,
  5. percent = value / sum(value) * 100)#相当于按照样本分组,然后计算比例
  6. #只提取需要的行
  7. data2a <- data2[data2$variable=="high",]
  8. data2a$percent <- round(data2a$percent,3)
head(data2a)
     ID variable value percent
1 samp1     high     1  16.667
3 samp2     high     2  33.333
5 samp3     high     3  50.000
7 samp4     high     4  66.667
9 samp5     high     5  83.333

绘图

  1. p1 <- ggplot(data = data,
  2. aes(x=ID,y=value,fill=variable))+
  3. #geom_bar(stat = "identity",position = "stack")+ ##展示原来数值
  4. geom_bar(stat = "identity",position = "fill")+ ##按照比例展示:纵坐标为1
  5. scale_y_continuous(expand = expansion(mult=c(0.01,0.1)),##展示纵坐标百分比数值
  6. labels = scales::percent_format())+
  7. scale_fill_manual(values = c("high"="#98d09d","low"="#e77381"), ##颜色调整
  8. limits=c("high","low"))+ ##limit调整图例顺序
  9. geom_text(data=data2a,
  10. aes(x=ID,y=1,
  11. label=paste0(value,"\n",#跨行
  12. "(",percent,")")),
  13. inherit.aes = FALSE,
  14. vjust=-0.2)+
  15. theme(panel.background = element_blank(), ##主题设置
  16. axis.line = element_line(),
  17. legend.position = "bottom")+
  18. labs(title = "Title",x=NULL,y="percent")+ ##X,Y轴设置
  19. guides(fill=guide_legend(title = NULL,nrow = 1,byrow = FALSE))
  20. p1
  21. #dev.off()

合图

  1. p2 <-p+ p1
  2. p2

补充:

跟着Nature学作图:R语言ggplot2堆积柱形图完整示例 - 简书 (jianshu.com)

《R数据可视化手册》——3.8 绘制百分比堆积条形图-阿里云开发者社区 (aliyun.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/444366
推荐阅读
相关标签
  

闽ICP备14008679号