2016-09-15 30 views
2

我想从一个小数据框中产生一个饼图。起初一切运作良好R - 奇怪的饼图在ggplot中的行为

library(ggplot2) 
library(data.table) 

c1 <- c(2,3) 
c2 <- c("second","third") 
c2 <- factor(c2, levels = c("first","second","third","fourth")) 
c3 <- c(0.7,0.3) 
cs <- data.frame(c1,c2,c3) 
ct <- data.table(cs) 
colx <- c("blue","red") 
midpoint <- cumsum(ct$c3) - ct$c3/2 

keycols = c("c1") 
setkeyv(ct,keycols) 
ct 



    c1 c2 c3 
1: 2 second 0.7 
2: 3 third 0.3 

vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) + 
      geom_bar(stat="identity",width=2) + 
      coord_polar(theta='y')+ 
      theme(axis.ticks=element_blank(), axis.title=element_blank(), 
      axis.text.y = element_blank(), panel.grid = element_blank(), 
      axis.text.x = element_text(color=colx,size=15,hjust=0))+ 

     scale_y_continuous(breaks = midpoint, labels = ct$c2) + 
     scale_fill_manual(values=colx) + 
     scale_x_continuous(limits=c(-1,2.5)) 
vysg 

enter image description here

的问题开始,当我需要新行添加到数据帧(data.table)(零个结果第一和第四)

ctlab <- data.table(levels(c2)) 
nlabs <- ctlab[!V1%in%ct$c2] 
nlabs[, V1 := factor(V1,levels=c("first","second","third","fourth"))] 
nct <- data.frame(c1=c(1,4),c2=nlabs[,V1],c3=0) 
ct <- rbind(ct,nct) 
colx <- c("green","blue","red","brown") 
ct$c2 <- factor(ct$c2,levels=c("first","second","third","fourth")) 
ct$c4 <- as.character(ct$c2) 
keycols = c("c1") 
setkeyv(ct, keycols) 
ct 

    c1  c2 c3  c4 
1: 1 first 0.0 first 
2: 2 second 0.7 second 
3: 3 third 0.3 third 
4: 4 fourth 0.0 fourth 

data.table看起来不错但图表不是

midpoint <- cumsum(ct$c3) - ct$c3/2 
vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) + 
    geom_bar(stat="identity",width=2) + 
    coord_polar(theta='y') + 
    theme(axis.ticks=element_blank(), axis.title = element_blank(), 
    axis.text.y = element_blank(), panel.grid = element_blank(), 
    axis.text.x=element_text(color=colx,size=15,hjust=0)) + 
    scale_y_continuous(breaks = midpoint, labels = ct$c2) + 
    scale_fill_manual(values = colx) + 
    scale_x_continuous(limits = c(-1,2.5)) 
vysg 

Warning message: 
In `[[<-.factor`(`*tmp*`, n, value = "first/fourth") : 
    invalid factor level, NA generated 

enter image description here

在标签C4(串)更换C2后警告将不会出现,但图表是不正常

midpoint <- cumsum(ct$c3) - ct$c3/2 
vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) + 
    geom_bar(stat="identity",width=2) + 
    coord_polar(theta = 'y') + 
    theme(axis.ticks=element_blank(), axis.title=element_blank(), 
    axis.text.y = element_blank(), panel.grid = element_blank(), 
    axis.text.x = element_text(color=colx,size=15,hjust=0)) + 
    scale_y_continuous(breaks = midpoint, labels = ct$c4) + 
    scale_fill_manual(values=colx) + 
    scale_x_continuous(limits=c(-1,2.5)) 
vysg 

enter image description here

我想这个问题是隐藏在因子(C2)但无法找到如何修正它的方法。我明确地设置了两个级别 - 旧数据框架和新数据框架。

+0

只是不做一个饼图。他们无可否认是有史以来最糟糕的图表 –

回答

5

你的问题是0°和360°是相同的角度,ggplot2知道这一点。你会看到,如果你只绘制这样的:

ggplot(ct, aes(x=1, y=c3, fill=c2)) + 
    geom_bar(stat="identity",width=2) + 
    coord_polar(theta='y') 

因此,你需要做的标签,一些准备:

midpoint[midpoint == 1] <- 0 
labs <- aggregate(ct$c2, list(midpoint), FUN = function(x) paste(x, collapse = "/")) 
vysg<-ggplot(ct, aes(x=1, y=c3, fill=c2)) + 
    geom_bar(stat="identity",width=2) + 
    coord_polar(theta='y')+ 
    theme(axis.ticks=element_blank(), axis.title=element_blank(), 
     axis.text.y=element_blank(), panel.grid = element_blank(), 
     axis.text.x=element_text(color=colx,size=15,hjust=0))+ 
    scale_y_continuous(breaks=labs$Group.1, labels=labs$x)+ 
    scale_fill_manual(values=colx)+ 
    scale_x_continuous(limits=c(-1,2.5)) 
vysg 

resulting plot

不同颜色的刻度值不可能的(没有在网格图形级别编辑)。也许scale_fill_manual(values=colx[as.integer(factor(midpoint))])将适合您的需求。

最后,强制性建议:通常比饼图更好的选项来说明这些数据。

+0

它的工作原理,谢谢! – Martin