绘制r中的同心饼图

时间:2014-11-26 18:54:02

标签: r pie-chart

我试图做一个同心饼图。内部饼图代表三类主题,每个类必须分成3个子类(当然,子类的切片必须与相应的内部切片一致)。 这就是我的尝试:

layout(matrix(c(1,1,1,1,2,1,1,1,1), nrow=3)); pie(x=c(14,22,15,3,15,33,0,6,45),labels="",col=c("#f21c39","#dba814","#7309de")); pie(x=c(51,51,51),labels=c("O","VG","V"),col=c("#c64719","#0600f5","#089c1f"))

这很有用,但内部馅饼太小了。我尝试使用radius选项,但外部切片与内部切片不对应。我该如何调整它们?

3 个答案:

答案 0 :(得分:5)

在这种情况下,使用par(new=TRUE)覆盖馅饼而不是layout()

pie(x=c(14,22,15,3,15,33,0,6,45),labels="",
    col=c("#f21c39","#dba814","#7309de"))
par(new=TRUE)
pie(x=c(51,51,51),labels=c("O","VG","V"),radius=.5,
    col=c("#c64719","#0600f5","#089c1f"))

enter image description here

答案 1 :(得分:4)

三年后。这可以使用sunburstR包来实现。 http://timelyportfolio.github.io/sunburstR/example_baseball.html

示例:

DF <- data.frame(LOGRECNO = c(60, 61, 62, 63, 64, 65),
             STATE = c(1, 1, 1, 1, 1, 1),
             COUNTY = c(1, 1, 1, 1, 1, 1), 
             TRACT = c(21100, 21100, 21100, 21100, 21100, 21100), 
             BLOCK = c(1053, 1054, 1055, 1056, 1057, 1058))
DF$BLOCKID <-
  paste(DF$LOGRECNO, DF$STATE, DF$COUNTY, 
        DF$TRACT, DF$BLOCK, sep = "-")

DF %>% 
  select(BLOCKID) %>% 
  group_by(BLOCKID) %>% 
  summarise(Tots=n())->dftest

sunburst(dftest)  

enter image description here enter image description here

我确定你能够根据自己的需要进行调整!

答案 2 :(得分:0)

你也可以使用ggsunburst包

# install ggsunburst
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("rPython")) install.packages("rPython")
install.packages("http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz", repos=NULL, type="source")
library(ggsunburst)

df <- read.table(header=T, text = "
parent node size
O 1 14
O 2 22
O 3 15
V 1 3
V 2 15
V 3 33
VG 1 1
VG 2 6
VG 3 45")

write.table(df, file = 'df.txt', sep = ',', row.names = F)

sb <- sunburst_data('df.txt', type = "node_parent", sep = ",")
p <- sunburst(sb, node_labels = T, leaf_labels = F, rects.fill.aes = "name")

cols <- c("O" = "#c64719", "V" = "#0600f5", "VG" = "#089c1f", "1" = "#f21c39", "2" = "#dba814", "3" = "#7309de")
p + scale_fill_manual(values = cols)

enter image description here