mtennekes / treemap

R package for treemap visualisation
64 stars 24 forks source link

Is there a way to automatically save my treemap as a simple png in my code? #53

Closed kamaulindhardt closed 3 years ago

kamaulindhardt commented 3 years ago

Hi,

I love the treemap as a cool initial explorative data analysis visualisation and the treemap function is excellent! I am currently building a website using Distill for RMarkdown and wish to include some of my treemaps, however, I cannot find a code-friendly way to save my treemap outputs into a png that I can plot on my website.

Any solution. Thank you!

Currently, I have this in my R markdown where I make a treemap, turn it into an interactive form using d3tree

{r Splitting ERA data and viewing proportions of practices for themes in all ERA data, code_folding=TRUE, layout="l-body-outset", fig.width=12, fig.height=10, fig.cap="Proportion of ERA theme and practices"}


# Lets split these
ERA.Compiled.Split <- ERAComboSplit(Data = ERAg::ERA.Compiled) # Using the ERAComboSplit function inherently from the ERAg package to “split” these practice and product combinations into duplicate individual rows each contain a unique combination of any practice x product combination present in the original observation.

# First we count the number of studies per level of the practice hierarchy (see PracticeCodes object for more information on the practice hierarchy)
ERA.Compiled.Split.Pracs <- ERA.Compiled.Split[,list(N.Studies=length(unique(Code))),by=list(SubPrName.Combo,PrName.Combo,Theme.Combo)]

# Visualize with the treemap function
# Proportions of ERA Theme data treemap 
# Using the "Set2" colour palette from RColorBrewer
ERA_tree_theme_practices <- (treemap::treemap(ERA.Compiled.Split.Pracs,
      index=c("Theme.Combo","PrName.Combo"),
      vSize="N.Studies",
      type="index",
      palette = "Set2",
      border.col=c("black","white"),             
      border.lwds=c(5,1), 
      fontsize.title=12,
      title="Proportion of ERA theme and practices based on the number of studies for each ERA theme and practice"))

# Interactive treemap using the d3treeR package function d3tree()
ERA_tree_theme_practices_d3viz <- d3tree(ERA_tree_theme_practices)

# saving the treemap using save_d3_html() function from the package r2d3
save_d3_png(ERA_tree_theme_practices_d3viz, file = "ERA_tree_theme_practices_d3viz.png")

and when I try to save the interactive d3 treemap I get this:

image

This was the only way I could find to save my treemap yet I am sure there is a more straight forward one..?

kamaulindhardt commented 3 years ago

I managed.. Not very intuitively but I managed 👏

# Visualize with the treemap function
# Proportions of ERA Theme data treemap 
# Using the "Set2" colour palette from RColorBrewer

png(filename="tree.png",width=800, height=800)
ERA_tree_theme_practices <- (treemap::treemap(ERA.Compiled.Split.Pracs,
      index=c("Theme.Combo","PrName.Combo"),
      vSize="N.Studies",
      type="index",
      palette = "Set2",
      border.col=c("black","white"),             
      border.lwds=c(5,1), 
      fontsize.title=12,
      title="Proportion of ERA theme and practices based on the number of studies for each ERA theme and practice"))

# Interactive treemap using the d3treeR package function d3tree()
ERA_tree_theme_practices_d3viz <- d3tree(ERA_tree_theme_practices,
                                         id = "name",
                                         celltext = "name",
                                         valueField = "size",
                                         width = 800, height = 600)

# saving the treemap using save_d3_html() function from the package r2d3
save_d3_png(ERA_tree_theme_practices_d3viz, 
            file = "ERA_tree_theme_practices_d3viz.png",
            delay = 2,
            zoom = 1
            )

This gave me two static treemaps:

image

and one interactive:

image