如何将生物图表保存为.png [英] How to save ggrough chart as .png

查看:97
本文介绍了如何将生物图表保存为.png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在使用Rggrough( https://xvrdm.github. io/ggrough/).我有以下代码(摘自该网页):

Say that I am using the R package ggrough (https://xvrdm.github.io/ggrough/). I have this code (taken from that webpage):

library(ggplot2)
library(ggrough)
count(mtcars, carb) %>%
    ggplot(aes(carb, n)) +
    geom_col() + 
    labs(title="Number of cars by carburator count") + 
    theme_grey(base_size = 16) -> p 
options <- list(
    Background=list(roughness=8),
    GeomCol=list(fill_style="zigzag", angle_noise=0.5, fill_weight=2))

然后我可以创建图表(我正在使用RStudio):

I can then create the chart (I am using RStudio):

get_rough_chart(p, options)

但是,我可以使用什么代码将其另存为.png文件?我正在尝试:

However, what code can I use to save it as a .png file? I am trying:

png("ggrough.png")
get_rough_chart(p, options)
dev.off()

我也尝试过:

x11()
get_rough_chart(p, options)

但这也不起作用(即使它确实在x11窗口中渲染,我也不知道如何将其另存为.png.

But this doesn't work either (and even if it did render in the x11 window, I don't know how to then save that as a .png.

如何将ggrough图另存为.png?

What should I do to save the ggrough plot as a .png?

推荐答案

ggrough图在本质上是一个htmlwidget,因此我认为典型的图像保存代码不起作用.

The ggrough plot is an htmlwidget at heart so I do not think typical image saving code will work.

如前所述,您可以通过htmlwidgets::saveWidget(rough_chart_object, "rough_chart.html")htmlwidgets保存到磁盘.这将创建一个具有html canvas元素的html文件,该元素是通过嵌入式javascript绘制的.正如您所注意到的,webshot::webshot()由于我还没有弄清楚的原因而无法捕获图像.

As mentioned, you can save htmlwidgets to disk via htmlwidgets::saveWidget(rough_chart_object, "rough_chart.html"). This creates an html file with an html canvas element that is drawn on via embedded javascript. As you noticed, webshot::webshot() is not able to capture the image for some reason I too have yet to figure out.

由于html文件可在Chrome中正确显示,因此我编写了这种RSelenium方法.但是,RSelenium很难在所有相互依赖的条件下运行,并且通过这种方法创建的映像可能需要后处理.也就是说,由于情节没有填充整个画布元素,因此图像中包含很多不良的空白.

Because the html file renders correctly in Chrome, I wrote up this RSelenium approach. However, RSelenium can be a pain to get running with all the inter-dependencies, and the image created via this approach may require post-processing. That is, because the plot does not fill the entire canvas element, the image contains a lot of undesirable white space.

但是我将把这种方法留给其他人考虑.

But I will leave this approach here for others to think about.

library(dplyr)
library(ggplot2)
library(ggrough)
library(RSelenium)
library(htmlwidgets)

# make ggplot
count(mtcars, carb) %>%
  ggplot(aes(carb, n)) +
  geom_col() + 
  labs(title="Number of cars by carburator count") + 
  theme_grey(base_size = 16) -> gg_obj

# convert to rough chart
options <- list(
  Background=list(roughness=8),
  GeomCol=list(fill_style="zigzag", angle_noise=0.5, fill_weight=2))

rough_chart <- get_rough_chart(p = gg_obj, rough_user_options = options)

# save rough chart
saveWidget(rough_chart, "rough_chart.html")

# start selenium driver
rd <- remoteDriver(
  remoteServerAddr = "localhost", 
  port = 4444L,
  browserName = "chrome"
)

rd$open()

# navigate to saved rough chart file
rd$navigate(paste0("file:///", getwd(), "/rough_chart.html"))

# find canvas element and size
canvas_element <- rd$findElement("id", "canvas")
canvas_size <- canvas_element$getElementSize()

# zoom to chart size with padding
rd$setWindowSize(canvas_size$width + 2 * canvas_size$x, 
                 canvas_size$height + 2 * canvas_size$y)

# save as png
rd$screenshot(file = "rough_chart.png")

# close chrome
rd$close()

这篇关于如何将生物图表保存为.png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆