R语言 多组堆砌图

目录

数据格式

普通绘图

添加比例

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

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

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

②#计算百分比##

data2 <- ddply(data,

"ID", ##需展示的X轴列名

transform,

percent = value / sum(value) * 100)#相当于按照样本分组,然后计算比例

#只提取需要的行

data2a <- data2data2$variable=="high",

data2apercent \<- round(data2apercent,3)

数据格式
复制代码
rm(list = ls())
library(ggplot2)
library(tidyverse)
library(reshape)
library(plyr)
library(patchwork)
dat <- as.data.frame(cbind(1:5, 5:1))
colnames(dat) <- c("high","low")
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

需要进行数据转换

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

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

添加比例

计算百分比

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

绘图

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

p1
#dev.off()

合图

复制代码
p2 <-p+ p1
p2

补充:

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

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

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
龙亘川2 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
龙亘川2 天前
一网统管AI平台民生业务实践:基于城市数字底座赋能公积金业务服务升级
大数据·安全·智慧城市·开源软件·数据可视化·政务
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int2 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
龙亘川2 天前
水利工程决策分析报表业务建模:从 “定时报表 + 自定义报表“ 到驾驶舱钻取闭环
人工智能·智慧城市·开源软件·数据可视化·水利水务
CoderYanger2 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法