[英]How to make ragg::agg_png device work with ggsave?
When I try to use the new-ish ragg::agg_png()
device with ggplot2::ggsave()
, the image does not appear to save correctly.当我尝试将新的
ragg::agg_png()
设备与ggplot2::ggsave()
一起使用时,图像似乎无法正确保存。
Take the following reprex.采取以下代表。 I make a simple plot and then save it using the
agg_png()
function directly, and with ggsave()
.我做了一个简单的 plot 然后直接使用
agg_png()
function 和ggsave()
保存它。 The image saved with the agg_png()
device directly comes out as expected.使用
agg_png()
设备保存的图像直接按预期输出。 However, when I use ggsave()
, it's almost like the units are being ignored.但是,当我使用
ggsave()
时,几乎就像忽略了单位一样。 You can't tell, but there is a tiny image beneath the final code output.你看不出来,但在最终代码 output 下方有一个小图像。 In that output, we can see that the image is only 7x7 px, even though inches have been specified by the units.
在 output 中,我们可以看到图像只有 7x7 像素,尽管单位已指定英寸。 From this blog post , it doesn't seem like anything extra should be required to make
ggsave()
work beyond setting device = agg_png
.从这篇博客文章来看,除了设置
device = agg_png
之外,似乎不需要任何额外的东西来使ggsave()
工作。
Are there additional parameters I need to specify?我需要指定其他参数吗? Including session info in case there is something system-specific going on.
包括 session 信息,以防发生系统特定的事情。
library(ggplot2)
library(magick)
library(ragg)
p <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point()
# save plot --------------------------------------------------------------------
agg_png(filename = "agg_png.png", width = 7, height = 7, units = "in",
res = 320)
print(p)
dev.off()
ggsave("agg_png-ggsave.png", plot = p, device = agg_png, width = 7, height = 7,
units = "in", dpi = 320)
# look at images ---------------------------------------------------------------
dev <- image_read("agg_png.png")
print(dev)
#> # A tibble: 1 x 7
#> format width height colorspace matte filesize density
#> <chr> <int> <int> <chr> <lgl> <int> <chr>
#> 1 PNG 2240 2240 sRGB FALSE 55978 126x126
ggs <- image_read("agg_png-ggsave.png")
print(ggs)
#> # A tibble: 1 x 7
#> format width height colorspace matte filesize density
#> <chr> <int> <int> <chr> <lgl> <int> <chr>
#> 1 PNG 7 7 sRGB FALSE 116 28x28
Created on 2021-03-01 by the reprex package (v1.0.0)由代表 package (v1.0.0) 于 2021 年 3 月 1 日创建
sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.4 (2021-02-15) #> os macOS Big Sur 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-03-01 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.2) #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.2) #> cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.4) #> colorspace 2.0-0 2020-11-11 [1] CRAN (R 4.0.2) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.2) #> curl 4.3 2019-12-02 [1] CRAN (R 4.0.1) #> DBI 1.1.1 2021-01-15 [1] CRAN (R 4.0.2) #> debugme 1.1.0 2017-10-22 [1] CRAN (R 4.0.2) #> digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2) #> dplyr 1.0.4 2021-02-02 [1] CRAN (R 4.0.2) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.1) #> fansi 0.4.2 2021-01-15 [1] CRAN (R 4.0.2) #> farver 2.1.0 2021-02-28 [1] CRAN (R 4.0.4) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) #> generics 0.1.0 2020-10-31 [1] CRAN (R 4.0.2) #> ggplot2 * 3.3.3.9000 2021-03-01 [1] Github (tidyverse/ggplot2@dbd7d79) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.2) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.2) #> htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.0.2) #> httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2) #> knitr 1.31.5 2021-02-24 [1] Github (yihui/knitr@ca09938) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.0.2) #> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.0.2) #> magick * 2.6.0 2021-01-13 [1] CRAN (R 4.0.2) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.2) #> mime 0.10 2021-02-13 [1] CRAN (R 4.0.2) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2) #> pillar 1.5.0 2021-02-22 [1] CRAN (R 4.0.4) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2) #> png 0.1-7 2013-12-03 [1] CRAN (R 4.0.2) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.2) #> R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.2) #> ragg * 1.1.0.9000 2021-03-01 [1] Github (r-lib/ragg@05bd5a7) #> Rcpp 1.0.6 2021-01-15 [1] CRAN (R 4.0.2) #> reprex 1.0.0 2021-01-27 [1] CRAN (R 4.0.2) #> rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.2) #> rmarkdown 2.7.2 2021-03-01 [1] Github (rstudio/rmarkdown@c0b8584) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.0.2) #> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.2) #> styler 1.3.2 2020-02-23 [1] CRAN (R 4.0.2) #> systemfonts 1.0.1 2021-02-09 [1] CRAN (R 4.0.2) #> textshaping 0.3.1 2021-02-22 [1] CRAN (R 4.0.4) #> tibble 3.1.0 2021-02-25 [1] CRAN (R 4.0.2) #> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.2) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 4.0.2) #> vctrs 0.3.6 2020-12-17 [1] CRAN (R 4.0.2) #> withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.2) #> xfun 0.21 2021-02-10 [1] CRAN (R 4.0.2) #> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.2) #> #> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
Default units for ragg
device is in px
. ragg
设备的默认单位是px
。 Change it to inches and try this将其更改为英寸并尝试此操作
p <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point()
png <- function(...) ragg::agg_png(..., res = 300, units = "in")
ggsave("agg_png-ggsave.png", plot = p, device = png, width = 3, height = 3, units = "in",
dpi = 100) # units = "in"
ggs <- image_read("agg_png-ggsave.png")
print(ggs)
Actually, you can just try this其实你可以试试这个
ggsave("agg_png-ggsave.png", p, device = ragg::agg_png, res = 300, units = "in")
ggs <- image_read("agg_png-ggsave.png")
print(ggs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.