圆环图与饼图类似,也是反映各个部分的占比情况,看各种类型的数据与整体之间的关系。下面将展示一下在R与python中的实现方法。
R中是没有封装好的包来直接实现的,我们将用ggplot2中的geom_bar进行极坐标变换得到。首先创建两组数:
data1 <- data.frame("cell1" = c(30,25, 66, 13, 23)/sum(c(30,25, 66, 13, 23)), 'type' = c('Intron', 'Intergenic',"UTR","Exon","CDS")) data2 <- data.frame("cell2" = c(29, 28, 90, 19, 31)/sum(c(29, 28, 90, 19, 31)), "type" = c('Intron', 'Intergenic',"UTR","Exon","CDS"))
Then draw the barplot of these two sets of numbers:
library(ggplot2) theme_set(theme_bw()) p <- ggplot() + geom_bar(data1, mapping = aes(x = 0.8, y = cell1, fill = type), color = "gray", stat ='identity', width = 0.4) + geom_bar(data2, mapping = aes(x = 0.5, y = cell2, fill = type), color ='gray',width = 0.3, stat ='identity') + labs(y ='cells', y ='percentage') p
image
After the polar coordinate changes, we can get:
p + coord_polar(theta='y')
image
It still doesn't look good now, and finally remove the xy axis scale and the title of the xy axis:
p + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), panel.border=element_blank(), panel.grid=element_blank(), axis.ticks = element_blank(), axis. text = element_blank()) + guides(fill = guide_legend(title ='type'))
image
2. Implementation of python (matplotlib.pyplot)
Python will use pyplot in matplotlib to draw two pie graphs to achieve.
import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.size'] = 7.0 # matplotlib sets the global font # Create two sets of data x1 = [30,25, 66, 13, 23] x2 = [29, 28, 90, 19, 31] x_0 = [1,0,0,0] #Used to display hollow Labels = ["Intron","Intergenic","UTR","Exon","CDS"] # Label colors = ["#FFDD55","#EE7700","#99FF99","#5599FF","#FF77FF"] # Corresponding colors # Used to set the font and size of the legend font1 = {'family':'Times New Roman', 'weight':'normal', 'size': 10, } # Create picture plt.figure(figsize=(8,8)) fig, ax = plt.subplots() #Make three pie pictures, the last one is used as the hollow in the middle pie_1 = ax.pie(x1,startangle = 90, radius=1.8, pctdistance = 0.9, colors=colors) pie_2 = ax.pie(x2,startangle = 90,radius=1.5,pctdistance = 0.9,colors=colors) pie_0 = ax.pie(x_0, radius=1.2,colors ='w') # Set image title ax.text(0.1, 2.1,'test', fontsize=18, style='oblique', ha='center',va='top',wrap=True) # Draw the color of the edge of each pie graph for pie_wedge in pie_1[0]: pie_wedge.set_edgecolor('gray') for pie_wedge in pie_2[0]: pie_wedge.set_edgecolor('gray') # Set the position and font of the legend ax.legend(labels, bbox_to_anchor=(1.3,1.0), loc='center left', prop=font1) # Set the figure to a circle ax.set(aspect="equal")
Finally, you can get:
image
To learn more about life letter/programming knowledge, you can follow the official account~