输入数据为一个向量,数值范围为0-100百分数:
现在我需要按照指定的间隔范围统计,数值出现的频率得到结果绘制饼图:
统计数据,以及ggplot2绘图饼图代码如下:
library(reshape2) local({r <- getOption("repos") ;r["CRAN"] <- "http://mirrors.tuna.tsinghua.edu.cn/CRAN/" ;options(repos=r)}) library(ggplot2) library(cowplot) library(RColorBrewer) pairjoinC <- function(x,y){
ran1=paste(paste(head(x,-1),"%",sep=""), paste(tail(x,-1),"%",sep=""), sep="-")
ran1[length(ran1)]=">40%"
ran1=paste(ran1," (",y,")",sep="")
ran1
}
# use the base hist() function to calculate BINs ,超过40%的统计到一起;
data[data>40]=41
MassStatC <- with(hist(data, breaks=seq(0, 45, by = 5), plot=FALSE), data.frame(N=counts, Mass=pairjoinC(breaks,counts), PCT=counts/sum(counts)))
MassStatC$Mass=factor(MassStatC$Mass, levels=rev(MassStatC$Mass), order=T)
pe = ggplot(MassStatC, aes(x="", y= N, fill= Mass))+
geom_bar(stat="identity",width=1)+
coord_polar(theta="y")+
labs(x="", y="",title="Distribution of Protein's Sequence Coverages")+
theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
scale_fill_brewer(palette="Paired",breaks = MassStatC$Mass, labels = as.character(MassStatC$Mass),direction=-1)+
geom_text(aes(x = 1.7, y = cumsum(MassStatC$N)-MassStatC$N/2 , label =paste( as.character(round(MassStatC$PCT*100,2)),"%",sep="")), show.legend = FALSE, color="black")+
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank(),
panel.background = element_rect(fill = "transparent",colour = NA),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.line= element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA))
pe
更多R语言绘图,可观看《R语言绘图视频课程》
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!