How to output editable pictures for PowerPoint in R language

created at 08-02-2021 views: 13

With the support of the ggplot2 package, the R language can make very beautiful statistical graphs. However, do you have such an experience that in order to make an ideal picture, you need to constantly adjust the drawing parameters, including color, font size, font format, and comment location, which is very troublesome. Especially for scientific research cooperation with others, the drawing of pictures requires constant communication, and even requires the use of tools such as PS and GIMP.

Have you ever thought that you can modify any element of the picture as shown in the animation below? ? ?

This article introduces a solution to you, you can save the pictures drawn in R language to the PPT file in the way of metadata (points, lines, surfaces, colors, shapes, etc.), and modify them in the PPT.

editable image for powerpoint

packages

  • officer package: A package for accessing and editing word and PPT files from the R side.
  • rvg package: a package for generating vector graphics for PPT and Excel.

Both packages can be installed from CRAN, and the installation names are as follows:

install.packages(c('officier', 'rvg'))

Implementation

1 R image format conversion
Through the conversion of the dml function in rvg, R can generate two types of graphs, Graphics and ggplot graphs, and convert them into a format that can be directly imported into PPT.

  • Graphics diagram: First find a piece of code that generates a decision curve
data(dcaData)
set.seed(123)
baseline.model <- decision_curve(Cancer~Age + Female + Smokes,
                                data = dcaData,
                                thresholds = seq(0, .4, by = .005),
                                bootstraps = 10)
full.model <- decision_curve(Cancer~Age + Female + Smokes + Marker1 + Marker2,
                            data = dcaData,
                            thresholds = seq(0, .4, by = .005),
                            bootstraps = 10)

plot_decision_curve( list(baseline.model, full.model),
                    curve.names = c("Baseline model", "Full model"),
                    col = c("blue", "red"),
                    lty = c(1,2),
                    lwd = c(3,2, 2, 1),
                    legend.position = "bottomright")

The last code segment is the command for drawing. We only need to perform the following operations on the last segment, which is to assign it to the code parameter of the dml function.

p.dca <- dml(code = {
  plot_decision_curve( list(baseline.model, full.model),
                    curve.names = c("Baseline model", "Full model"),
                    col = c("blue", "red"),
                    lty = c(1,2),
                    lwd = c(3,2, 2, 1),
                    legend.position = "bottomright")
})
  • ggplot chart: also find a piece of code that generates a frequency distribution contrast histogram:
ggbarstats(
  data = mtcars,
  x = vs,
  y = cyl
)

Make the following modifications to the drawing code and assign it to the ggobj parameter in the dml function:

p.bar <- dml(ggobj = {
  ggbarstats(
  data = mtcars,
  x = vs,
  y = cyl
)
})

2 Import PPT
Use the officier package to output the previously generated p.dca and p.bar to the PPT. The following  functions are used:
3 Open PPT--read_pptx
4 Add page--add_slide
5 Import picture--ph_with
6 Save PPT--print
The specific operations are as follows:

pptx <- read_pptx()
pptx <- add_slide(pptx)
ph_with(pptx, value = p.dca, 
        location = ph_location_type(type = 'body'))

pptx <- add_slide(pptx)
ph_with(pptx, value = p.bar, 
        location = ph_location_fullsize())

print(pptx, 'output.pptx')
created at:08-02-2021
edited at: 08-02-2021: