R绘制扇形图
作者:互联网
今天跟大家分享如何用R绘制扇形图(饼图)
作图数据是这样的:
具体步骤:
1.pie()绘制饼图
打开R,先设置饼图颜色,接着读取目标文件的数据,调用pie()绘制样本a1中主要细菌类别的丰度组成,具体代码如下所示:
color=c( "#3C5488B2","#00A087B2",
"#F39B7FB2","#91D1C2B2",
"#8491B4B2", "#DC0000B2",
"#7E6148B2","yellow",
"darkolivegreen1", "lightskyblue",
"darkgreen", "deeppink", "khaki2",
"firebrick", "brown1", "darkorange1",
"cyan1", "royalblue4", "darksalmon",
"darkgoldenrod1", "darkseagreen", "darkorchid")
phylum <- read.delim('phylum.txt', sep = '\t', stringsAsFactors = FALSE, check.names = FALSE)
#pie(),可使用 ?pie 查看该命令详情
pie(phylum$a1, col =color[1:length(unique(phylum$phylum))],
labels = phylum$phylum, font = 3, main = 'Sample: a1\nPhylum level')
作图结果如下:
2.pie3D()绘制3D饼图
使用第一步的测试数据,调用plotrix包(该包可绘制3D效果的饼图),输入命令pie3D(),具体代码如下所示:
#pie3D(),可使用 ?pie3D 查看该命令详情
library(plotrix)
pie3D(phylum$a1, col = c('#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3', '#FDB462'),
explode = 0.05, height = 0.1, radius = 0.85, labels = phylum$phylum, labelcex = 1, main = 'Sample: a1\nPhylum level')
作图结果如下:
3.fan.plot()叠加扇形图
继上步读入的测试数据,调用plotrix包中fan.plot()命令,可进一步绘制叠加的扇形图,具体代码如下所示:
#fan.plot(),可使用 ?fan.plot 查看该命令详情
library(plotrix)
fan.plot(phylum$a1, col = c('#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3', '#FDB462'),
labels = phylum$phylum, main = 'Sample: a1\nPhylum level')
作图结果如下:
4.ggplot2绘制饼图
相信大家对这个包比较熟悉,上篇推文咱们就介绍过,当然它也可以同样用来绘制饼图,具体代码如下:
#ggplot2
library(ggplot2)
p <- ggplot(phylum, aes(x = '', y = a1, fill = phylum)) +
geom_bar(stat = 'identity', width = 1) +
coord_polar(theta = 'y') +
scale_fill_manual(values = rev(c('#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3', '#FDB462'))) +
theme(panel.grid = element_blank(), panel.background = element_blank(), axis.text.x = element_blank(), plot.title = element_text(hjust = 0.5)) +
theme(legend.text = element_text(face = 'italic'), legend.title = element_blank()) +
labs(x = '', y = '', title = 'Sample: a1', fill = 'Phylum')
p
作图结果如下:
小伙伴想了解更多绘图软件,可关注“作图帮”公众号,或微信扫码下方二维码进群,在线为您解答科研绘图相关问题!
标签:plot,作图,pie3D,phylum,fan,扇形,绘制 来源: https://blog.csdn.net/weifanbio/article/details/115077030