16

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

在此处输入图像描述

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

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

4 回答 4

5

那张图太棒了。
好吧,在我开始有点疯狂之前,我已经走了这么远。简而言之,有一种快速的、非 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)

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

在此处输入图像描述

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')

这给了我们:

在此处输入图像描述

嗯,这和我得到的一样接近。向任何可以在 R 中重新创建该图的人致敬!

于 2016-08-14T19:33:01.200 回答
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"))

在此处输入图像描述

于 2018-05-05T09:31:34.973 回答
2

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

于 2013-06-12T17:09:42.317 回答
2

这当然是可能的,尽管没有任何罐头函数可以为你做这件事。

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

如果您真的需要外边缘周围的图像,请查看该rasterImage功能。

您也可以只计算每个部分的多边形坐标并绘制它们,将其包装在适当的循环和函数中是合理的。

正如评论中提到的那样,使用带有堆积条和极坐标的 ggplot2 可能对您有用。

于 2013-06-12T18:11:00.167 回答