English 中文(简体)
如何用 R 的军官将 ggplot 输入 Word Doc?
原标题:How to put a ggplot into a Word doc using Officer in R?
  • 时间:2024-07-24 20:06:55
  •  标签:
  • r
  • officer
I am attempting to use bookmarks in Word to automatically place plots into a report. I have already used "body_replace_flextable_at_bkm" to add flextables automatically, however I cannot find a function to do so for plots. I saw "replace_external_img" but not plot! Here is what I tried, but obviously it did not work because the plots are not external images: template <- read_docx("C:/Users/Documents/template.docx") body_replace_img_at_bkm(template,"plot1",plot1) # bookmark is named plot1, as well as the R plot print(template,target="C:/Users/Documents/template.docx") Output: Error in body_replace_img_at_bkm(template, "plot1", plot1) : inherits(value, "external_img") is not TRUE
问题回答
While the flextable package provides a convenience function to replace a bookmark with a flextable object, the officer only provides functions to replace a bookmark with text or an image. But one option to achieve your desired result would be to save your plot to a temporary file and then replace the bookmark with the image file using body_replace_img_at_bkm. UPDATE After a look at how body_replace_flextable_at_bkm is implemented, a shorter approach would be to use cursor_bookmark and body_add_gg where for the latter we have to use pos = "on" to replace the bkm with the plot: template <- read_docx() template <- body_add_par(template, "[Insert plot here]") template <- body_bookmark(template, "plot1") template <- body_add_par(template, "Bla Bla Bla") template <- cursor_bookmark(template, "plot1") template <- body_add_gg( x = template, value = plot1, width = 16 / 2.54, height = 9 / 2.54, pos = "on" ) print(template, target = "template1.docx") And similar to the edit by @Axeman we can wrap this in a function: body_replace_gg_at_bkm <- function(x, bookmark, value, width = 6, height = 5, res = 300, style = "Normal", scale = 1, ...) { x <- cursor_bookmark(x, bookmark) x <- body_add_gg( x = x, value = value, width = width, height = height, res = res, style = style, scale = scale, pos = "on", ... ) x } Original Post Using a minimal reproducible example based on the default docx template shipped with officer: library(ggplot2) library(officer) plot1 <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) + geom_point() template <- read_docx() template <- body_add_par(template, "[Insert plot here]") template <- body_bookmark(template, "plot1") template <- body_replace_img_at_bkm( template, "plot1", external_img( src = ggsave( tempfile(fileext = ".png"), plot1, width = 16, height = 9, unit = "cm" ), width = 16, height = 9, unit = "cm" ) ) # bookmark is named plot1, as well as the R plot print(template, target = "template.docx") If you do this sort of thing more often, you can define a simple function to do this for you, e.g.: body_replace_img_with_plot_at_bkm <- function( x, bookmark, plot, width = 16, height = 9 # in cm ) { body_replace_img_at_bkm( x, bookmark, external_img( src = ggsave( tempfile(fileext = ".png"), plot, width = width, height = height, unit = "cm" ), width = width, height = height, unit = "cm" ) ) } Then use as: template <- body_replace_img_with_plot_at_bkm(template, "plot1", plot1)




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签