其他
ggplot 绘制环形堆叠条形图
river flows in you
1、引言
有时候如果 分类太多 ,绘制堆叠条形图就会很长,我们可以绘制成环形就可以充分节约空间和版面费了。
今天分享一下如何绘制 环形的条形堆叠图 。
2、构造数据
# 加载R包
library(reshape2)
library(tidyverse)
library(ggnewscale)
library(ggplot2)
# 构造数据
set.seed(123)
df <- as.data.frame(matrix(data = sample(0:50,400,replace = T),ncol = 5))
# 给列命名
colnames(df) <- c('Heterogeneous selection','Homogeneous selection',
'Dispersal limitation','Homogenizing dispersal',
'Undominated')
# 添加分类
df$x <- factor(rownames(df),levels = rownames(df))
df$type <- rep(LETTERS[1:20],4)
df$sample <- c(rep('Soil',20),rep('Water',20),rep('Leaf',20),rep('Root',20))
# 查看数据
head(df,3)
Heterogeneous selection Homogeneous selection Dispersal limitation Homogenizing dispersal Undominated
1 30 4 12 26 47
2 14 50 18 0 1
3 50 24 46 25 46
x type sample
1 1 A Soil
2 2 B Soil
3 3 C Soil
da <- melt(df)
# 查看数据
head(da,3)
x type sample variable value
1 1 A Soil Heterogeneous selection 30
2 2 B Soil Heterogeneous selection 14
3 3 C Soil Heterogeneous selection 50
3、绘图
画个普通版看看:
# 普通版
ggplot(data = da) +
# 柱形图层
geom_col(aes(x = x,y = value,fill = variable),
# color ='black',
position = position_fill()) +
# 颜色
scale_fill_brewer(palette = 'Set1')
是不是特别长,我们变成环形看看:
# 环形
p <- ggplot(data = da) +
# 柱形图层
geom_col(aes(x = x,y = value,fill = variable),
# color ='black',
position = position_fill()) +
# 颜色
scale_fill_brewer(palette = 'Set1') +
# 掰弯
coord_polar(theta = 'x') +
# 主题细节调整
theme_classic(base_size = 18) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank()) +
xlab('') + ylab('')
p
变成空心的环:
# 空心环
p +
scale_y_continuous(expand = expansion(add = c(0.75,0)))
还可以添加注释:
# 添加注释
p +
scale_y_continuous(expand = expansion(add = c(0.75,0.05))) +
# 新图例
new_scale('fill') +
geom_col(aes(x = x,y = -0.01,fill = sample),
width = 1) +
scale_fill_brewer(palette = 'Paired')
还可以在外圈添加注释:
# 外部注释数据
anno = data.frame(xmin = c(0.55,20.55,40.55,60.55),
xmax = c(20.45,40.45,60.45,80.45),
type = LETTERS[1:4])
anno
xmin xmax type
1 0.55 20.45 A
2 20.55 40.45 B
3 40.55 60.45 C
4 60.55 80.45 D
# 添加外部注释
p +
scale_y_continuous(expand = expansion(add = c(0.75,0.05))) +
# 新图例
new_scale('fill') +
geom_col(aes(x = x,y = -0.015,fill = sample),
width = 1) +
scale_fill_brewer(palette = 'Paired') +
# 新图例
new_scale('fill') +
# 外部注释图层
geom_rect(data = anno,aes(xmin = xmin ,xmax = xmax,
ymin = 1.03,ymax = 1.08,
fill = type))
还可以把 position 模式改为 stack :
# stack模式
ggplot(data = da) +
# 柱形图层
geom_col(aes(x = x,y = value,fill = variable),
# color ='black',
width = 0.9,
position = position_stack()) +
# 颜色
scale_fill_brewer(palette = 'Set1') +
# 掰弯
coord_polar(theta = 'x') +
# 主题细节调整
theme_classic(base_size = 18) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank()) +
xlab('') + ylab('') +
scale_y_continuous(expand = expansion(add = c(80,0))) +
# 新图例
new_scale('fill') +
geom_col(aes(x = x,y = -1,fill = sample),
width = 1.5) +
scale_fill_brewer(palette = 'Paired') +
# 新图例
new_scale('fill') +
# 外部注释图层
geom_rect(data = anno,aes(xmin = xmin ,xmax = xmax,
ymin = 220,ymax = 230,
fill = type))
或者对我们的分类变量进行 分面 :
# 分面
ggplot(data = da) +
# 柱形图层
geom_col(aes(x = type,y = value,fill = variable),
# color ='black',
width = 0.9,
position = position_fill()) +
# 颜色
scale_fill_brewer(palette = 'Set1') +
facet_wrap(~sample,ncol = 4) +
# 掰弯
coord_polar(theta = 'x') +
# 主题细节调整
theme_minimal(base_size = 18) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank()) +
xlab('') + ylab('')
画个空心的分面:
# 空心分面
ggplot(data = da) +
# 柱形图层
geom_col(aes(x = type,y = value,fill = variable),
# color ='black',
width = 0.9,
position = position_fill()) +
# 颜色
scale_fill_brewer(palette = 'Set1') +
facet_wrap(~sample,ncol = 2) +
# 掰弯
coord_polar(theta = 'x') +
# 主题细节调整
theme_classic(base_size = 18) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank()) +
xlab('') + ylab('') +
scale_y_continuous(expand = expansion(add = c(0.75,0.05)))
4、中山陵晚景
明明是花儿,却像雪一样。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦。
群二维码:
老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀Ribo-seq 质控软件:ribosomeProfilingQC
◀...