R绘制多重饼图

转载自R语言ggplot2绘制饼图+甜甜圈图 - 简书

代码如下:

library(ggplot2) # 绘图
 library(ggsci) # 配色
 
 # 构建测试数据
 dat = data.frame(x = rep('b',7),
  y = rep('a',7),
  z = rep('c',7),
  cat1 = paste('c',1:7, sep = '_'),
  cat2 = c('a1','a1','a2','a2','a3','a4','a4'),
  value1 = 1:7,
  value2 = 1:7)
 
 # 分别求所占百分比
 dat1 = aggregate(dat$value1, by = list(dat$cat1), FUN = sum)
 dat1$per1 = dat1$x / sum(dat1$x)
 
 # for循环构建标签的相对位置
 for (i in seq(nrow(dat1), 1)) {
  if (i == nrow(dat1)) {
  dat1$per.y1[i] = dat1$per1[i] / 2
  }else{
  dat1$per.y1[i] = sum(dat1$per1[(i + 1):nrow(dat1)]) + dat1$per1[i] / 2
  }
 }
 
 # 构建标签后合并数据
 dat1$label1 = paste(dat1$Group.1,'(',round(dat1$per1*100, 2),'%',')', sep = '')
 dat = merge(dat, dat1[,c(1,3,4,5)], by.x = 'cat1', by.y = 'Group.1')
 
 # 重复操作
 dat2 = aggregate(dat$value2, by = list(dat$cat2), FUN = sum)
 dat2$per2 = dat2$x / sum(dat2$x)
 
 for (i in seq(nrow(dat2), 1)) {
  if (i == nrow(dat2)) {
  dat2$per.y2[i] = dat2$per2[i] / 2
  }else{
  dat2$per.y2[i] = sum(dat2$per2[(i + 1):nrow(dat2)]) + dat2$per2[i] / 2
  }
 }
 
 dat2$label2 = paste(dat2$Group.1,'(',round(dat2$per2*100, 2),'%',')', sep = '')
 dat = merge(dat, dat2[,c(1,3,4,5)], by.x = 'cat2', by.y = 'Group.1')
 
 # 绘图
 ggplot(dat) +
  # 绘制柱状图
  geom_bar(aes(y, 
  ifelse(cat2 == 'a3', per2, per2/2), 
  fill = cat2),
  stat = 'identity', width = 1.3) +
  # 添加标签
  geom_text(aes(1.25, as.numeric(per.y2), 
  label = label2),
  size =2.5, color = 'black') +
  # 绘制柱状图
  geom_bar(aes(x, per1, fill = cat1), 
  stat = 'identity', width = .8, color = 'white') +
  # 添加标签
  geom_text(aes(2, as.numeric(per.y1),label = label1),
  size = 2.5, color = 'black') +
  # 设置Y轴刻度
  scale_y_continuous(labels = scales::percent) +
  coord_polar(theta = "y") + # 转换坐标轴
  theme_void() +
  scale_fill_igv() + # 设置填充色
  theme(legend.position = 'none') # 隐藏图例

自己使用的代码如下!!!看懂自用

### 多重饼图
library(ggplot2) # 绘图
library(ggsci) # 配色
# cell type&lineage 两个条件提取目标数据,过滤掉细胞数为0的分组
data = table(ob.merge.rmdb$lineage, ob.merge.rmdb$pie_celltype) %>% as.data.frame() %>%
  rename("lineage"="Var1","cell_type"="Var2","num"="Freq")
data = subset(data, num > 0)
# 第一次提取celltype和lineage细胞数量,并排序
data_lineage = aggregate(data$num, by = list(data$lineage), FUN = sum)
colnames(data_lineage)=c("lineage", "num_lin")
data_celltype = aggregate(data$num, by = list(data$cell_type), FUN = sum)
colnames(data_celltype)=c("cell_type", "num_type")
# 合并之后排序
data = merge(data, data_lineage, by.x = 'lineage', by.y = 'lineage', sort = FALSE)
data = merge(data, data_celltype, by.x = 'cell_type', by.y = 'cell_type', sort = FALSE)
data = arrange(data, -num_lin, -num_type)
data = data[,1:3]
# 根据排序指定data不同变量的factor
data$cell_type = factor(data$cell_type, levels = data$cell_type)
data$cell_type
data$lineage = factor(data$lineage, levels = unique(data$lineage))
data$lineage


# 明确排序后第二次提取cell type,计算各细胞类型比例
data_celltype = aggregate(data$num, by = list(data$cell_type), FUN = sum)
colnames(data_celltype)=c("cell_type", "num_type")
data_celltype$per_type = data_celltype$num_type/sum(data_celltype$num_type)
# for循环构建标签的相对位置
# 计算原理:data中的第一行位于图中最上方
# 第i行纵坐标位置应为((i+1):n)+1/2i, except i = n
for (i in seq(nrow(data_celltype), 1)) {
  if (i == nrow(data_celltype)) {
    data_celltype$per.type.y[i] = data_celltype$per_type[i]/2
  }else{
    data_celltype$per.type.y[i] = (sum(
      data_celltype$per_type[(i + 1):nrow(data_celltype)])+data_celltype$per_type[i]/2)
  }
}
# 生成标签
data_celltype$label_type = 
  paste(data_celltype$cell_type,' (',round(data_celltype$per_type*100, 2),'%',')', sep = '')
data = merge(data, data_celltype, by.x = 'cell_type', by.y = 'cell_type', sort = FALSE)

# 第二次提取lineage,计算各谱系类型比例
data_lineage = aggregate(data$num, by = list(data$lineage), FUN = sum)
colnames(data_lineage)=c("lineage", "num_lin")
data_lineage$per_lin = data_lineage$num_lin/sum(data_lineage$num_lin)
# for循环构建标签的相对位置
for (i in seq(nrow(data_lineage), 1)) {
  if (i == nrow(data_lineage)) {
    data_lineage$per.lin.y[i] = data_lineage$per_lin[i]/2
  }else{
    data_lineage$per.lin.y[i] = (sum(
      data_lineage$per_lin[(i + 1):nrow(data_lineage)]) + data_lineage$per_lin[i]/2)
  }
}
# 生成标签
data_lineage$label_lin = 
  paste(data_lineage$lineage,' (',round(data_lineage$per_lin*100, 2),'%',')', sep = '')
# 合并
data = merge(data, data_lineage, by.x = 'lineage', by.y = 'lineage', sort = FALSE)
# 因为在lineage中许多lineage重叠次数不一样,需要根据相应lineage比例,计算纵坐标位置
# 而celltype因为都是唯一存在,所以不用
table(ob.merge.rmdb@meta.data$lineage)
# 预设lineage平均比例为NA,之后通过循环填充
data$per_lin_mean="NA"
dim(data)
for(i in 1:nrow(data)){
  if (data[i,"lineage"]=="Progenitor and stem cell"){
    data[i,"per_lin_mean"]=data$per_lin[i]/9
  }else if (data[i,"lineage"]=="Adipo-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/2
  }else if (data[i,"lineage"]== "Osteo-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/2
  }else if (data[i,"lineage"]== "Vascular-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/2
  }else if (data[i,"lineage"]=="Chondro-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/3
  }else if (data[i,"lineage"]=="Myo-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/4
  }else if (data[i,"lineage"]=="Hemo-lineage") {
    data[i,"per_lin_mean"]=data$per_lin[i]/7
  }else {
    data[i,"per_lin_mean"]=data$per_lin[i]
  }
}
data$per_lin_mean=as.numeric(data$per_lin_mean)
colnames(data)

# 画图
# 指定Lineage列和celltype列在图中的位置
# 并不是先画lineaeg,就在图上先出现lineage
# 而是根据lable的字母顺序展现的
multi_pie = ggplot(data) +
  geom_bar(aes(1, per_lin_mean, fill = lineage),stat = 'identity',width = 1.2) +
  geom_text(aes(1, as.numeric(per.lin.y), label = label_lin),
            size =2.5, color = 'black') +
  geom_bar(aes(2, per_type, fill = cell_type),stat = 'identity', width = 0.8)+
  geom_text(aes(3, as.numeric(per.type.y),label = label_type),
            size = 2.5, color = 'black') +
  scale_y_continuous(labels = scales::percent) +
  coord_polar(theta = "y", start=0, direction = -1) + # 转换坐标轴
  theme_void() + #A completely empty theme.
  scale_fill_igv()+ # 设置填充色
  theme(legend.position = 'none')
multi_pie
ggsave("multi_pie.png", plot=multi_pie, device = "png") # 默认保存最近一次画图

# or
ggplot(data) +
  geom_bar(aes(Lin, Num, fill = Lineage),stat = 'identity',width = 1, position = "fill") +
  geom_bar(aes(type, Num, fill = Cell_type),stat = 'identity', width = 1.5, position = "fill")

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值