View source: R/zz-api-file-output-utils.R
gg_save_as | R Documentation |
Saves a ggplot object to disk at a set physical size. Allows specific maximum dimensions with an optional target aspect ratio to fit into specific configurations for publication. e.g. a half page plot or a third of a 2 column page. Allows output in pdf for journal publication or png for inclusion in documents, and makes sure that the outputs are near identical.
gg_save_as(...)
... |
Arguments passed on to
|
For maximum cross platform reproducibility we are using the combination of 'systemfonts' for font management, 'svglite' to render the canonical output 'rsvg' to convert that to pdf, and 'ragg' to for bitmap formats. In some situations 'rsvg' fails in which case we fall back to rendering in a headless chrome instance. This rather complicated pipeline ensures modern webfont support, and editable SVG or PDF.
the output is an sensible default object that can be displayed given the context it is called in, for example if knitting an RMarkdown document a link to the png file for embedding, if latex a link to the pdf file.
try({
.gg_pedantic(fontSize = 6)
p = ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt, colour=as.factor(cyl))) +
ggplot2::geom_point()
# p %>% .gg_save_as(filename="~/tmp/plot_example",maxWidth=4,maxHeight=4)
p %>% .gg_save_as(filename=tempfile(),maxWidth=2,maxHeight=1.5)
plot = ggplot2::ggplot(ggplot2::diamonds, ggplot2::aes(x=carat,y=price,color = color))+
ggplot2::geom_point()+
ggplot2::annotate("label",x=2,y=10000,label="Hello \u2014 world", family="Kings")+
ggplot2::labs(tag = "A")+
ggplot2::xlab("Carat\u2082")+
ggplot2::ylab("price\u2265")
# plot %>% .gg_save_as(filename="~/tmp/plot_example_2")
res = plot %>% .gg_save_as(filename=tempfile(), formats=c("png","eps"))
as.character(res)
res
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.