R - ggplot中

时间:2016-09-15 14:56:20

标签: r ggplot2

我正在尝试从小数据框生成饼图。起初一切都运作良好

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)中,却找不到如何修改它的方法。我明确地设置了两个级别 - 旧数据框架和新数据框架。

1 个答案:

答案 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))])可以满足您的需求。

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