如何将ggrough图表另存为.png
假设我正在使用如何将ggrough图表另存为.png,r,ggplot2,rstudio,png,x11,R,Ggplot2,Rstudio,Png,X11,假设我正在使用R包ggrough()。我有以下代码(取自该网页): 但是,我可以使用什么代码将其保存为.png文件?我正在努力: png("ggrough.png") get_rough_chart(p, options) dev.off() 我还尝试: x11() get_rough_chart(p, options) 但这也不起作用(即使它在x11窗口中渲染,我也不知道如何将其保存为.png) 如何将ggrough绘图保存为.png?绘图本质上是htmlwidget,因此我认为典型的图像
R
包ggrough
()。我有以下代码(取自该网页):
但是,我可以使用什么代码将其保存为.png文件?我正在努力:
png("ggrough.png")
get_rough_chart(p, options)
dev.off()
我还尝试:
x11()
get_rough_chart(p, options)
但这也不起作用(即使它在x11窗口中渲染,我也不知道如何将其保存为.png)
如何将
ggrough
绘图保存为.png?绘图本质上是htmlwidget
,因此我认为典型的图像保存代码不起作用
如前所述,您可以通过htmlwidgets::saveWidget(rough\u chart\u object,“rough\u chart.html”)
将htmlwidgets
保存到磁盘。这将创建一个html文件,其中包含一个通过嵌入javascript绘制的html画布元素。正如您所注意到的,webshot::webshot()
无法捕获图像,原因我也没有弄清楚
因为html文件在Chrome中正确呈现,所以我编写了这个RSelenium方法。然而,RSelenium在所有相互依赖的情况下运行可能会很困难,并且通过这个方法创建的图像可能需要后处理。也就是说,因为绘图没有填充整个画布元素,所以图像包含很多不需要的内容空间
但我将把这个方法留给其他人思考
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()
库(dplyr)
图书馆(GG2)
图书馆(ggrough)
图书馆(资源库)
库(htmlwidgets)
#作图
计数(mtcars,carb)%>%
ggplot(aes(碳、氮))+
geom_col()+
实验室(title=“按化油器计数的汽车数量”)+
主题灰(基本尺寸=16)->gg\U obj
#转换为粗略图表
选项ggrough
绘图本质上是一个htmlwidget
,因此我认为典型的图像保存代码不起作用
如前所述,您可以通过htmlwidgets::saveWidget(rough\u chart\u object,“rough\u chart.html”)
将htmlwidgets
保存到磁盘。这将创建一个html文件,其中包含一个通过嵌入javascript绘制的html画布元素。正如您所注意到的,webshot::webshot()
无法捕获图像,原因我也没有弄清楚
因为html文件在Chrome中正确呈现,所以我编写了这个RSelenium方法。然而,RSelenium在所有相互依赖的情况下运行可能会很困难,并且通过这个方法创建的图像可能需要后处理。也就是说,因为绘图没有填充整个画布元素,所以图像包含很多不需要的内容空间
但我将把这个方法留给其他人思考
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()
库(dplyr)
图书馆(GG2)
图书馆(ggrough)
图书馆(资源库)
库(htmlwidgets)
#作图
计数(mtcars,carb)%>%
ggplot(aes(碳、氮))+
geom_col()+
实验室(title=“按化油器计数的汽车数量”)+
主题灰(基本尺寸=16)->gg\U obj
#转换为粗略图表
选项说明基于,我尝试了:aMaybeggsave()
?我刚刚尝试了ggsave()
,但得到了这个错误:“UseMethod中的错误(“grid.draw”):没有适用于“grid.draw”的方法应用于类“c('ggrough',htmlwidget')”的对象。注意,基于此,我尝试了:aMaybeggsave()
?我刚刚尝试了ggsave()
,但出现了以下错误:“UseMethod中的错误(“grid.draw”):没有适用于“grid.draw”的方法应用于类“c('ggrough','htmlwidget')”的对象”
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()