ggplot2 绘制环形图

1. 效果图

PMID=34911763
在这里插入图片描述

2. 模仿

(1) 准备数据

Input =("
  row   mapType   pct rep
  1                  'Uniquely mapped reads %' 78.74   1
  2       '% of reads mapped to multiple loci' 16.99   1
  3       '% of reads mapped to too many loci'  2.68   1
  4 '% of reads unmapped: too many mismatches'  0.00   1
  5           '% of reads unmapped: too short'  0.42   1
  6               '% of reads unmapped: other'  1.17   1
")
dat = as.data.frame(read.table(textConnection(Input),
                              header=TRUE,
                              row.names=1, sep = ""))
dat$type2=paste0("c", 1:nrow(dat))
str(dat)
dat2=dat[, c("type2", "pct")]
dat2

> dat2
  type2   pct
1    c1 78.74
2    c2 16.99
3    c3  2.68
4    c4  0.00
5    c5  0.42
6    c6  1.17

(2)整理数据:对比例列计算累计比例

dat2$fraction = dat2$pct / sum(dat2$pct)  # 计算比例
dat2$ymax = cumsum(dat2$fraction)         # 计算累积比例
dat2$ymin = c(0, head(dat2$ymax, n=-1))   # 计算最小值
dat2$label_y = (dat2$ymax + dat2$ymin) / 2  # 计算标签的 y 位置
dat2

> dat2
  type2   pct fraction   ymax   ymin label_y
1    c1 78.74   0.7874 0.7874 0.0000 0.39370
2    c2 16.99   0.1699 0.9573 0.7874 0.87235
3    c3  2.68   0.0268 0.9841 0.9573 0.97070
4    c4  0.00   0.0000 0.9841 0.9841 0.98410
5    c5  0.42   0.0042 0.9883 0.9841 0.98620
6    c6  1.17   0.0117 1.0000 0.9883 0.99415

(3) 绘制一个堆积柱状图

library(ggplot2)

colorset.RNA=c("#6E7AA3", "#68CDE0", "#FF8166", "#FFB7A2", "#C175B3", "#3CACDD", "#24C6A6")

# 分解步骤1
ggplot(dat2, aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3, fill = type2)) +
  geom_rect()+
  #coord_polar(theta = "y") +
  xlim(c(2, 4))+
  scale_fill_manual(values=colorset.RNA)  # 填充颜色

在这里插入图片描述

(4) 极坐标环化:最终效果

# 绘制环形图
p1=ggplot(dat2, aes(ymax = ymax, ymin = ymin, 
                 xmax = 4, xmin = 3, #环的x轴范围
                 fill = type2)) +
  geom_rect() +
  coord_polar(theta = "y", direction = -1) +    # 转换为极坐标
  xlim(c(2, 4.1)) +   # 设置环的大小, [1]内部的空心圆;[2]扩大外部范围,为文字留空间
  theme_void(base_size = 14) +      # 主题设置
  labs(title = "RNA percentage") +                 # 添加标题
  #scale_fill_brewer(palette = "Set3", direction = -1)  # 选择调色板
  scale_fill_manual(values=colorset.RNA)+  # 填充颜色

  ggrepel::geom_text_repel(aes(x=3.8, y = label_y, label = type2), # 添加标签
                              size = 7,  #标签字体大小
                              color = "black",                # 设置标签颜色
                              segment.color = "red",        # 设置连线颜色
                              segment.size = 2,            # 设置连线粗细
                              nudge_x = 0.3,                 # 设置连线长度
                              point.padding = 0.8); p1            # 设置点的填充

在这里插入图片描述

Ref

  • []
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值