r - R/graphics:在函数中绘制为 PNG
问题描述
我想使用一个函数cowplot::plot_grid()
直接写入PNG。我可以通过脚本使它工作:
g <- ggplot2::ggplot(datasets::iris, ggplot2::aes(x = Sepal.Length, y= Sepal.Width)) + ggplot2::geom_point()
grDevices::png(filename = "mypic1.png", width = 1000, height = 1000, units = "px", bg = "transparent")
cowplot::plot_grid(g)
grDevices::dev.off()
但是,如果我在一个函数中尝试这个,它不起作用:
pxPrint <- function(ggobject, fn, x = 1000, y = 1000) {
grDevices::png(filename = fn, width = x, height = y, units = "px", bg = "transparent")
cowplot::plot_grid(ggobject)
grDevices::dev.off()
}
pxPrint(ggobject = g, fn = "mypic2.png")
这会产生与脚本相同的消息...
RStudioGD
2
...但没有创建文件 mypic2.png。
假设这可能与文件位置有关,我在默认 WD 中的 RStudio 项目之外尝试了相同的代码。同样,mypic1.png 是在标准目录中创建的,但不是 mypic2。显示的消息是不同的(在这两种情况下):
null device
1
我错过了什么?
解决方案
请让我知道这是你所期待的。
g <- ggplot2::ggplot(datasets::iris, ggplot2::aes(x = Sepal.Length, y= Sepal.Width)) + ggplot2::geom_point()
pxPrint <- function(ggobject, fn, x = 1000, y = 1000) {
grDevices::png(filename = fn, width = x, height = y, units = "px", bg = "transparent")
print((cowplot::plot_grid(ggobject)))
grDevices::dev.off()
}
pxPrint(ggobject = g, fn = "mypic2.png")
原因:在您自己的函数中,您将需要一个显式print()
声明。
推荐阅读
- r - 读取 SVG 路径/多边形的坐标
- flutter - Flutter Hive:deleteAt 将值设为 null 而不是删除它
- vb.net - FileSystem.WriteAllText 是否有用于编辑文本文件的替代方法,我需要保留特殊字符
- python-3.x - 转换 dtype= 的变量
我正在使用 pandas 从 csv 读取值,我遇到了一种我以前从未见过的格式,举个例子:
myval = array('[1.0, 0, 0, 0]', dtype='<U14')
我想将 myval 转换为整数/浮点数列表,但我尝试的任何方法似乎都与这种数据类型的变量不兼容。将不胜感激任何帮助。
< - laravel - PHP Laravel 应用程序 (Ubuntu) 中的 AWS 凭证异常
- c# - 具有不同变量名的对象的 C# 模板类
- flutter - 在 Flutter Web 应用程序中更改屏幕尺寸
- json - 基于最小值的 JSON Schema 依赖
- python - 从Python中的Excel单元格中提取数字
- python - Matplotlib 在设置 x/y 限制后“擦除”行