Making a circular barplot with a hollow center (aka race track plot)

前端 未结 4 795
滥情空心
滥情空心 2021-01-30 13:22

I was asked to recreate the following style of plot. (Please ignore the question of whether this is a good type of visualization and charitably consider this as adding a colorf

4条回答
  •  长发绾君心
    2021-01-30 14:08

    Another base solution that doesn't rely on plotrix package:

    circular.barplot<-function(values, labels, col, cex){
        df<-data.frame(values=sort(values), labels=labels[order(values)])
        col<-col[order(values)]
        plot(NA,xlim=c(-1.3,1.3),ylim=c(-1.3,1.3),axes=F, xlab=NA, ylab=NA, asp=1)
        t<-sapply(df$values,function(x).5*pi-seq(0, 2*pi*x/100,length=1000))
        x<-sapply(1:nrow(df),function(x)(.3+x/nrow(df))*cos(t[,x]))
        y<-sapply(1:nrow(df),function(x)(.3+x/nrow(df))*sin(t[,x]))
        for(i in 1:nrow(df)){
            lines(x=x[,i],y=y[,i],col=col[i],lwd=10,lend=1)
            text(x[1,i],y[1,i],paste(df$labels[i]," - ",df$values[i],"%",sep=""),
                 pos=2,cex=cex)
            }
        }
    

    enter image description here

提交回复
热议问题