Saving Voronoi treemap as high resolution JPG or PNG image in R

I'm creating a Voronoi tree map using R and I want to save the map as a JPG or PNG image with high resolution in order to publish it in a journal. Below I reported my script to generate the map, could someone help me to modify the scripp adding line to save the map? Thank you for your help.

library(voronoiTreemap)
library(scales)
data<-read.csv("C:/File_script/2-subfamily-gene-regions-population-percentage-voronoi.csv", sep=",", header=T)
data$h1<-as.character(data$h1)
data$h2<-as.character(data$h2)
data$h3<-as.character(data$h3)
data$color<-as.character(data$color)
data$weight<-as.numeric(data$weight)
data$codes<-as.character(data$codes)
dataH <- vt_input_from_df(data)
plot <- vt_d3(vt_export_json(dataH), legend =FALSE, color_circle = "#080708", color_border = "#ffffff")

I leave also an example that you can replicated and try to save.

data(canada)
canada$codes <- canada$h3
canada <- canada[canada$h1=="Canada",]
canadaH <- vt_input_from_df(canada,scaleToPerc = FALSE, hierachyVar0 = "h1",
                           hierachyVar1 = "h2", hierachyVar2 = "h3", colorVar = "color", labelVar = "codes",
                            weightVar = "weight")
canadaH <- vt_input_from_df(canada,scaleToPerc = FALSE)
vt_d3(vt_export_json(canadaH))

This works for me

library(voronoiTreemap)

data(canada)
canada$codes <- canada$h3
canada <- canada[canada$h1=="Canada",]

canadaH <- vt_input_from_df(canada,scaleToPerc = FALSE)
myd3 <- vt_d3(vt_export_json(canadaH))

library(htmlwidgets)
saveWidget(myd3,"myd3.html",selfcontained = TRUE)

library(webshot2) #renv::install("rstudio/webshot2")

webshot("myd3.html","mypng.png",delay = 1,selector = "#htmlwidget_container")

Thanks, I've tried to use this method but I have some problems with the function webshot2
this is the message when I try to install it:
Warning in install.packages :
package 'webshot2' is not available for this version of R

That message is because you are probably using install.packages("webshot2") and webshot2 is not yet on CRAN.
its on https://github.com/rstudio/webshot2
Hence why I shared the install code using my favoured package manager ("renv" ) . renv is on CRAN so you could install that first and go from there.
if you have remotes package you could also do remotes::install_github("rstudio/webshot2") but I prefer renv.

In this way I have generate only an html file. If I capture the screen does the resolution of the image remain high or decreases?

That implies you used savewidget but not also webshot?

Yes I use both but with the last comand (webshot(.....)) I have this message:

webshot("myd3.html","mypng.png",delay = 1,selector = "#htmlwidget_container")
Error in getCurrentRegistryId() :
function 'Rcpp_precious_remove' not provided by package 'Rcpp'

Seems like you need an updated version of Rcpp

install.packages("Rcpp")

Ok! It works well, but the resolution obtained is the higest or there are some other possibilities to increse the resolution?

webshot has parameters for vwidth , vheight, zoom.
You should read the documentation
?webshot
and experiment with these

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.