分层/多级饼图

时间:2013-06-12 15:28:56

标签: r plot ggplot2 pie-chart

有没有办法在R中创建这样的图表?

enter image description here

以下是图表中显示的数据摘录:

df <- structure(list(Animal = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Buffalo", 
"Goat", "Sheep"), class = "factor"), Texture = structure(c(4L, 
4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 1L, 1L, 4L, 3L, 4L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L), .Label = c("Hard", "Semi-Hard", "Semi-Soft", 
"Soft"), class = "factor"), Name = structure(c(16L, 9L, 3L, 21L, 
5L, 4L, 10L, 2L, 12L, 11L, 8L, 14L, 1L, 7L, 22L, 15L, 6L, 20L, 
18L, 17L, 19L, 13L), .Label = c("Buffalo Blue", "Charolais", 
"Chevre Bucheron", "Clochette", "Crottin de Chavignol", "Feta", 
"Fleur du Maquis", "Garrotxa", "Golden Cross", "Humboldt Fog", 
"Idaho Goatster", "Majorero", "Manchego", "Mozzarella di Bufala Campana", 
"Ossau-Iraty", "Pantysgawn", "Pecorino Romano", "Pecorino Sardo", 
"Roncal", "Roquefort", "Sainte-Maure de Touraine", "Yorkshire Blue"
), class = "factor")), .Names = c("Animal", "Texture", "Name"
), class = "data.frame", row.names = c(NA, -22L))

4 个答案:

答案 0 :(得分:5)

该图表 awesome 好吧,在我开始有点疯狂之前,我已经做到了这一点。简而言之,有一种快速的非ggplot2方式(几乎可以提供你想要的东西)和一种长的ggplot2方式(几乎给你你想要的东西)。快速方式(使用上面作为示例数据给出的df):

devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
df1 <- df %>% 
  group_by(Animal) %>% 
  unite(col=Type, Animal:Name, sep = "-", remove=T)

df1$Type <- gsub(" ", "", df1$Type)
df1$Index <- 1
sunburst(df1)

这为您提供了一个很棒的交互式图像(这里不是交互式的,只是一个快照):

enter image description here

ggplot2的方法很棘手,我还没弄清楚如何在图像上正确注释,但是也许有人可以在此代码的基础上进行构建。

df1 <- df %>% 
  mutate(Colour = ifelse(.$Animal == "Goat", "#CD9B1D", ifelse(.$Animal == "Sheep", "#EEC900", "#FFD700"))) %>% 
  mutate(Index = 1) %>% 
  group_by(Animal)

有三层:

First <- ggplot(df1) + geom_bar(aes(x=1, y=Animal, fill=Animal, 
         label = Animal), position='stack', stat='identity', size=0.15) 
       + theme(panel.grid = element_blank(), axis.title=element_blank(), 
         legend.position="none", axis.ticks=element_blank(), 
         axis.text = element_blank())

Second <- First 
        + geom_bar(data=df1, aes(x=2, y=Animal, fill=Texture, group=Animal),
          position='stack', stat='identity', size=0.15, colour = "black") 
        + scale_color_brewer(palette = "YlOrBr")
        + scale_fill_brewer(palette = "YlOrBr") 
        + theme(axis.title=element_blank(), legend.position="none",    
          axis.ticks=element_blank(), axis.text = element_blank())

Third <- Second + geom_bar(data=df1, aes(x=3, y=Animal, fill=Name), 
         position='stack', stat='identity', size=0.15, colour = "black") 
       + scale_fill_manual(values = c("#EEC900", "#FFD700", "#CD9B1D",
         "#FFD700", "#DAA520", "#EEB422", "#FFC125", "#8B6914", "#EEC591", 
         "#FFF8DC", "#EEDFCC", "#FFFAF0", "#EEC900", "#FFD700", "#CDAD00", 
         "#FFF68F", "#FFEC8B", "#FAFAD2", "#FFFFE0", "#CD853F", "#EED8AE", 
         "#F5DEB3", "#FFFFFF", "#FFFACD", "#D9D9D9", "#EE7600", "#FF7F00",
         "#FFB90F", "#FFFFFF")) 
        + theme(axis.title=element_blank(), legend.position="none",
          axis.ticks=element_blank(), axis.text.y = element_blank(), 
          panel.background = element_rect(fill = "black"))

Third + coord_polar('y')

这给了我们:

enter image description here

嗯,那和我一样接近。对于任何可以在R !!中重新创建该图表的人来说,都是严肃的帽子。

答案 1 :(得分:3)

使用ggsunburst包

可以非常接近
# using your data
df <- structure(list(Animal = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Buffalo",
"Goat", "Sheep"), class = "factor"), Texture = structure(c(4L,
4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 1L, 1L, 4L, 3L, 4L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L), .Label = c("Hard", "Semi-Hard", "Semi-Soft",
"Soft"), class = "factor"), Name = structure(c(16L, 9L, 3L, 21L,
5L, 4L, 10L, 2L, 12L, 11L, 8L, 14L, 1L, 7L, 22L, 15L, 6L, 20L,
18L, 17L, 19L, 13L), .Label = c("Buffalo Blue", "Charolais",
"Chevre Bucheron", "Clochette", "Crottin de Chavignol", "Feta",
"Fleur du Maquis", "Garrotxa", "Golden Cross", "Humboldt Fog",
"Idaho Goatster", "Majorero", "Manchego", "Mozzarella di Bufala Campana",
"Ossau-Iraty", "Pantysgawn", "Pecorino Romano", "Pecorino Sardo",
"Roncal", "Roquefort", "Sainte-Maure de Touraine", "Yorkshire Blue"
), class = "factor")), .Names = c("Animal", "Texture", "Name"
), class = "data.frame", row.names = c(NA, -22L))

# add special attribute "dist" using "->" as sep, this will increase the size of the terminal nodes to make space for the cheese names
df$Name <- paste(df$Name, "dist:3", sep="->")

# save data.frame into csv without row and col names
write.table(df, file = 'df.csv', sep = ",", col.names = F, row.names = F)

# install ggsunburst package
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)

# generate data structure from csv and plot
sb <- sunburst_data('df.csv', type = "lineage", sep=",")
sunburst(sb, node_labels = T, node_labels.min = 15, rects.fill.aes = "name") + 
scale_fill_manual(values = c("#EEC900", "#FFD700", "#CD9B1D",
                           "#FFD700", "#DAA520", "#EEB422", "#FFC125", "#8B6914", "#EEC591", 
                           "#FFF8DC", "#EEDFCC", "#FFFAF0", "#EEC900", "#FFD700", "#CDAD00", 
                           "#FFF68F", "#FFEC8B", "#FAFAD2", "#FFFFE0", "#CD853F", "#EED8AE", 
                           "#F5DEB3", "#FFFFFF", "#FFFACD", "#D9D9D9", "#EE7600", "#FF7F00",
                           "#FFB90F", "#FFFFFF"), guide = F) +
theme(panel.background = element_rect(fill = "black"))

enter image description here

答案 2 :(得分:2)

使用示例数据集回答您的问题会更容易。但是,我建议您使用ggplot()和geom_bar()绘制堆叠条形图并添加+ coord_polar()。

答案 3 :(得分:2)

这肯定是可能的,虽然没有任何罐头功能准备好为你做。

我可能会从plotrix包中的floating.pie函数开始,创建一个具有最多细节的饼图(外环),然后在另一个饼图上绘制带有硬信息和软信息的饼图较小的直径使外环仍然显示但新的外环覆盖中心。然后最后用动物信息在中心做另一个较小的饼图。

如果您确实需要外边缘周围的图像,请查看rasterImage函数。

你也可以只计算每个部分的多边形坐标并绘制那些,在正确的循环和函数中包装它是合理的。

如评论中所述,同样使用带有堆积条和极坐标的ggplot2可能对您有用。