0

I've been looking to create a multilevel pie-chart (or doughnut chart) in R and the best I found was the package sunburstR, which I must say is a very promising tool.

The interactive functionality is great - however I don't really need it. I'd like to add a title and counts in the legend object and export the graph to an image format. Does this require advanced html coding? There is not much help material about this package on the web yet. Should I look into a difference package? The pie() function is for single level data and the example of geom_polar of ggplot2 I found on this forum does not seem to be appropriate for factors.

Here is an example of my dataset and sunburstR object - however my question is more general in nature and not specific to this example.

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)

Any help or suggestion would be appreciated. Thank you.

Mathieu
  • 67
  • 8

2 Answers2

2

I will add an example just in case you might want to pursue this option, but it seems you would like to avoid extra coding. 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")

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()
}
"
)

screenshot of sunburst

timelyportfolio
  • 6,479
  • 30
  • 33
0

You can generate a sunburst using the ggsunburst package. It is based on ggplot2, so you can use ggsave to export as image.

Here there is an example using your data. All the information can be included in the plot, so I removed the legend

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

enter image description here

didac
  • 311
  • 2
  • 4