R-ggplot饼图,带面_包裹

R-ggplot饼图,带面_包裹,r,ggplot2,R,Ggplot2,我是否正确理解不可能完全使用带有饼图(ggplot+coord_polar)的切面包装 库(ggplot2) 库(数据表) c1根据Cartlefish44有用的注释,代码如下所示 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("fir

我是否正确理解不可能完全使用带有饼图(ggplot+coord_polar)的切面包装

库(ggplot2)
库(数据表)

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

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
库(ggplot2)
库(数据表)

c1很容易使用
geom_text
,例如
+geom_text(aes(x=2.5,y=中点,label=c2,color=c2))
谢谢。我已将您的改进作为答案发布了解决方案。它工作得很好。如果你愿意,自己发布,我会删除我的版本。只是想展示结果,你不必这么做。您可以使用以下更大的值来更改饼图和标签之间的距离:
scale\u x\u continuous(limits=c(-1,xxx))
geom\u text(aes(x=xxx,y=…)
。您可以通过
geom_text(aes(…),fontface=“bold”)
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