【问题标题】:R - ggplot pie chart with facet_wrapR - 带有 facet_wrap 的 ggplot 饼图
【发布时间】:2017-02-26 13:56:25
【问题描述】:

我是否正确理解无法将 facet_wrap 与饼图 (ggplot + coord_polar) 完全结合使用?

library(ggplot2)
library(data.table)

c1 <- c(1,2,3,1,2,3)
c2 <- c("first","second","third","first","second","third")
c2 <- factor(c2, levels = c("first","second","third"))
c3 <- c(0.2,0.3,0.5,0.4,0.5,0.1)
c4 <- c("A","A","A","B","B","B")
c4 <- factor(c4, levels = c("A","B"))
cs <- data.frame(c1,c2,c3,c4)
ct <- data.table(cs)
ct[,midpoint:=cumsum(c3) - c3/2,by=c4]

colx <- c("blue","yellow","green")
ct[,colx:=colx,by=c4]

   c1     c2  c3 c4 midpoint   colx
1:  1  first 0.2  A     0.10   blue
2:  2 second 0.3  A     0.35 yellow
3:  3  third 0.5  A     0.75  green
4:  1  first 0.4  B     0.20   blue
5:  2 second 0.5  B     0.65 yellow
6:  3  third 0.1  B     0.95  green


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=ct[,colx],size=15,hjust=0))+ 
        scale_y_continuous(breaks = ct[,midpoint], labels = ct[,c2])  + 
        scale_fill_manual(values=ct[,colx]) +
        scale_x_continuous(limits=c(-1,2.5))
vysg<-vysg+facet_wrap(~ c4)
vysg

共享似乎正确,但标签及其位置不正确。 有什么方法可以使用 facetting 还是必须使用网格?

而且我知道饼图不是最好的。

【问题讨论】:

  • geom_text就好了,比如+ geom_text(aes(x = 2.5, y = midpoint, label = c2, colour = c2))
  • 谢谢。我已经发布了您的改进作为答案的解决方案。它完美地工作。如果您愿意,请自己发布,我将删除我的版本。只是想显示结果。
  • 您不必这样做。您可以通过使用更大的值来更改饼图和标签之间的距离,例如 xxx、scale_x_continuous(limits=c(-1, xxx))geom_text(aes(x = xxx, y = ...))。您可以使用geom_text(aes(...), fontface = "bold") 的粗体字体

标签: r ggplot2


【解决方案1】:

根据 cuttlefish44 有用的注释,代码如下所示

library(ggplot2)
library(data.table)

c1 <- c(1,2,3,1,2,3)
c2 <- c("first","second","third","first","second","third")
c2 <- factor(c2, levels = c("first","second","third"))
c3 <- c(0.2,0.3,0.5,0.4,0.5,0.1)
c4 <- c("A","A","A","B","B","B")
c4 <- factor(c4, levels = c("A","B"))
cs <- data.frame(c1,c2,c3,c4)
ct <- data.table(cs)
ct[,midpoint:=cumsum(c3) - c3/2,by=c4]

colx <- c("blue","yellow","green")
ct[,colx:=colx,by=c4]
ct

   c1     c2  c3 c4 midpoint   colx
1:  1  first 0.2  A     0.10   blue
2:  2 second 0.3  A     0.35 yellow
3:  3  third 0.5  A     0.75  green
4:  1  first 0.4  B     0.20   blue
5:  2 second 0.5  B     0.65 yellow
6:  3  third 0.1  B     0.95  green


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(),axis.text.x = element_blank(), panel.grid  = element_blank())+
        geom_text(aes(x = 2.5, y = midpoint, label = c2, colour = I(colx)))+
        scale_x_continuous(limits=c(-1,2.5))+
        scale_fill_manual(values=colx)
vysg<-vysg+facet_wrap(~ c4)
vysg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多