如何向sunburstR图形添加标题并将其导出为.png或.jpeg

如何向sunburstR图形添加标题并将其导出为.png或.jpeg,r,ggplot2,pie-chart,sunburst-diagram,htmlwidgets,R,Ggplot2,Pie Chart,Sunburst Diagram,Htmlwidgets,我一直在寻找在R中创建多级饼图(或甜甜圈图),我发现最好的是sunburstR包,我必须说这是一个非常有前途的工具 交互功能非常好,但是我并不真正需要它。我想在图例对象中添加标题和计数,并将图形导出为图像格式。这需要高级html编码吗?关于这个软件包,目前网上还没有太多的帮助资料。我应该看看不同的包装吗?pie()函数用于单级数据,我在本论坛上找到的ggplot2的geom_polar示例似乎不适用于因子 这是我的dataset和sunburstR对象的一个示例-但是我的问题本质上更一般,而不是

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

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

这是我的dataset和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)

如有任何帮助或建议,将不胜感激。谢谢。

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

data=gg=structure(列表)V1=structure(c(2L,1L,3L,4L,8L,5L,6L,7L
),.Label=c(“松树软”、“松树硬”,
“松树长”、“松树未定”、“枫树红”,
“枫树绿”、“枫树黄”,
“枫树美味”,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我将添加一个示例,以防您可能希望采用此选项,但似乎您希望避免额外的编码。 需要(sunburstR)

data=gg=structure(列表)V1=structure(c(2L,1L,3L,4L,8L,5L,6L,7L
),.Label=c(“松树软”、“松树硬”,
“松树长”、“松树未定”、“枫树红”,
“枫树绿”、“枫树黄”,
“枫树美味”,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您可以使用ggsunburst包生成sunburst。 它基于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)
#安装ggsunburst软件包
如果(!require(“ggplot2”))安装.packages(“ggplot2”)
如果(!require(“rPython”))安装.packages(“rPython”)
安装程序包(“http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz“,repos=NULL,type=“source”)
图书馆(ggsunburst)

df您可以使用ggsunburst包生成sunburst。 它基于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)
#安装ggsunburst软件包
如果(!require(“ggplot2”))安装.packages(“ggplot2”)
如果(!require(“rPython”))安装.packages(“rPython”)
安装程序包(“http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz“,repos=NULL,type=“source”)
图书馆(ggsunburst)

df我想给图表本身添加一个标题,而不是图例对象(为了清楚起见),这可以做到,但确实需要一些额外的JavaScript编码。我想给图表本身添加一个标题,而不是图例对象(为了清楚起见),这可以做到,但确实需要一些额外的JavaScript编码。