0

我一直在寻找在 R 中创建多级饼图(或圆环图),我发现最好的是 sunburstR 包,我必须说它是一个非常有前途的工具。

交互功能很棒——但我真的不需要它。我想在图例对象中添加标题和计数,并将图形导出为图像格式。这需要高级 html 编码吗?网上还没有太多关于这个包的帮助材料。我应该研究一个不同的包吗?pie() 函数用于单级数据,我在这个论坛上找到的 ggplot2 的 geom_polar 示例似乎不适用于因子。

这是我的数据集和 sunburstR 对象的示例-但是我的问题本质上更笼统,并不特定于该示例。

require(sunburstR)

data = gg=structure(list(V1 = structure(c(2L, 1L, 3L, 4L, 8L, 5L, 6L, 7L
), .Label = c("Pine Tree-Soft", "Pine Tree-Hard", 
"Pine Tree-Long", "Pine Tree-Undecided", "Maple Tree-Red", 
"Maple Tree-Green", "Maple Tree-Yellow", 
"Maple Tree-Delicious"), class = "factor"), V2 = c(3L, 
5L, 2L, 1L, 10L, 5L, 3L, 2L)), .Names = c("V1", "V2"), row.names = c(NA, 
-8L), class = "data.frame")

sunburst(data)

任何帮助或建议将不胜感激。谢谢你。

4

2 回答 2

2

我将添加一个示例以防万一您可能想要使用此选项,但您似乎希望避免额外的编码。需要(朝阳)

data = gg=structure(list(V1 = structure(c(2L, 1L, 3L, 4L, 8L, 5L, 6L, 7L
), .Label = c("Pine Tree-Soft", "Pine Tree-Hard", 
              "Pine Tree-Long", "Pine Tree-Undecided", "Maple Tree-Red", 
              "Maple Tree-Green", "Maple Tree-Yellow", 
              "Maple Tree-Delicious"), class = "factor"), V2 = c(3L, 
                                                                 5L, 2L, 1L, 10L, 5L, 3L, 2L)), .Names = c("V1", "V2"), row.names = c(NA, 
                                                                                                                                      -8L), class = "data.frame")

sb <- sunburst(
  data,
  count = TRUE, # add count just for demonstration
  legend = list(w=250), # make extra room for our legend
  legendOrder = unique(unlist(strsplit(as.character(data$V1),"-")))
)

# for the coding part
#  to add some additional information in the legend,
#  force show the legend,
#  and disable toggling of the legend

htmlwidgets::onRender(
  sb,
"
function(el,x) {
  // force show the legend
  //   check legend
  d3.select(el).select('.sunburst-togglelegend').property('checked',true);
  //   simulate click
  d3.select(el).select('.sunburst-togglelegend').on('click')();

  // change the text in the legend to add count
  d3.select(el).selectAll('.sunburst-legend text')
    .text(function(d) {return d.name + ' ' + d.value})

  // remove the legend toggle
  d3.select(el).select('.sunburst-togglelegend').remove()
}
"
)

朝阳的截图

于 2017-04-19T14:47:49.793 回答
0

您可以使用 ggsunburst 包生成旭日形。它基于 ggplot2,因此您可以使用 ggsave 导出为图像。

这里有一个使用您的数据的示例。所有信息都可以包含在情节中,所以我删除了图例

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


df <- read.table(header = T, text = "
parent node size
Pine Hard  3
Pine Soft  5
Pine Long  2
Pine Undecided  1
Maple Delicious 10
Maple Red  5
Maple Green  3
Maple Yellow  2
")

write.table(df, 'df.csv', sep = ",", row.names = F)

sb <- sunburst_data('df.csv', type = "node_parent", sep = ",", node_attributes = "size")
p <- sunburst(sb, node_labels = T, leaf_labels = F, rects.fill.aes = "name") + 
  geom_text(data = sb$leaf_labels, 
            aes(x=x, y=y, label=paste(label, size, sep="\n"), angle=angle), size = 2) +
  scale_fill_discrete(guide = F)

ggsave('sunburst.png', plot = p, w=4, h=4)

在此处输入图像描述

于 2018-05-06T13:15:08.880 回答