tomwenseleers / export

R package for streamlined export of graphs and data tables.
192 stars 35 forks source link

using `graph2ppt()` with {showtext} #45

Open SchmidtPaul opened 2 years ago

SchmidtPaul commented 2 years ago

Hi, thanks for the package - been using it for a while now. I recently started using {showtext} to make use of extra fonts in my ggplots. However, I noticed that after running the required showtext::showtext_auto(), the exported .pptx have issues with their text - see the screenshot below.

library(export)
library(ggplot2)
library(showtext)
showtext::showtext_auto()

p <- ggplot(data = PlantGrowth,
            aes(y = weight, x = group)) +
  geom_point() +
  labs(title = "This is a relevant title",
       subtitle = "Here is also a subtitle",
       caption = "And here is a caption")

p

export::graph2ppt(p, file = "Rplot")
#> Exported graph as Rplot.pptx

Created on 2022-01-19 by the reprex package (v2.0.1)

This is a Screenshot of Rplot.pptx: image

Ultimately, I have these questions:

  1. Is there a fix for this so that I can use {showtext} in combination with graph2ppt()?
  2. Taking a step back - the only reason I am exporting to pptx in the first place is to get the figures in my docx files to be as crisp-looking as possible. I achieve this via copying the graph from the pptx file and pasting it into word as an enhanced metafile. Does this sound reasonable to you or do you have a better suggestion?
  3. I usually export my plots as pdf, png and pptx. The first two I could do via ggsave(), but I actually only do so for pdf and subsequently run pdftools::pdf_render_page(pdf_path, page = 1, dpi = 300) %>% png::writePNG(bitmap, png_path) (see my custom export function). This seems to do the trick of always having the same font sizes and scales for both of these two exported files. Does this sound reasonable to you? Should I try to take a similar approach here with e.g. pdf2pptx::pdf2pptx()?
cvanderaa commented 2 years ago

Hello @SchmidtPaul,

Sorry for the very late answer. Hopefully this hasn't blocked you from generating your reports....

Here are my thoughts on your questions:

  1. Could it be that the font you are using with showtext is not available from PPT (and hence it takes some default font)? When you look select the text in your PPT, does it mention the expected font style?
  2. Did you have a look at export::graph2doc()? By default vector.graphic = TRUE meaning that vector graphics are used to draw the image and show yield crisp images in your docx. If you want to avoid copy pasting from one file to another, there is also the argument append = TRUE that will put your figure and the end of the target document.
  3. I'm not sure I'm understanding what you are doing. If your objective is to get the same image dimension with the same font, shapes, etc dimensions, you might give a look at the height and width arguments in graph2pdf, graph2ppt and graph2png (or even in ggsave).
SchmidtPaul commented 2 years ago

Hi, thanks for the answer!

First of all, I realized that the (3.) was actually more of a {showtext} issue and was addressed in yixuan/showtext#51. Here is a reprex that works fine for all p1 but has considerable dimension changes for p2:

library(export)
library(ggplot2)

p <- ggplot(data = PlantGrowth,
            aes(y = weight, x = group)) +
  geom_point() +
  labs(subtitle = "Here is also a subtitle",
       caption = "And here is a caption")

p1 <- p + ggtitle("Without showtext::showtext_auto()")
ggsave("p1.pdf", p1)
#> Saving 7 x 5 in image
ggsave("p1.png", p1)
#> Saving 7 x 5 in image
graph2doc(p1, "p1.docx", vector.graphic = TRUE)
#> Exported graph as p1.docx
graph2ppt(p1, "p1.pptx", vector.graphic = TRUE)
#> Exported graph as p1.pptx

showtext::showtext_auto()

p2 <- p + ggtitle("With showtext::showtext_auto()")
ggsave("p2.pdf", p2)
#> Saving 7 x 5 in image
ggsave("p2.png", p2)
#> Saving 7 x 5 in image
graph2doc(p2, "p2.docx", vector.graphic = TRUE)
#> Exported graph as p2.docx
graph2ppt(p2, "p2.pptx", vector.graphic = TRUE)
#> Exported graph as p2.pptx

Created on 2022-07-29 by the reprex package (v2.0.1)

For comparison, here are screenshots of all exported p2: image

Again, the font size/dimension thing can be ignored for now since all p1 do look identical. Yet, as you can see the problem remains for the pptx and I wouldn't assume that PowerPoint does not have this font, as I simply left it with the default (=Arial?). However, I will take your advice and switch to export::graph2doc(vector.graphic = TRUE, append = TRUE) now.

cvanderaa commented 2 years ago

Hmm indeed this is not expected. When I run your example p1 and p2 lead to the same results for all 4 formats on my side. I must say I don't have MicrosofOffice, but use LibreOffice instead, and am using an Ubuntu OS. Maybe the issue lies within PPT (cf also issue #49 ) ?

However, I cannot explain why the PDF resolution is different... Does it lead to the same with graph2pdf()? It is relying on cairo() whereas ggsave() relies on grDevices::pdf, although I don't know if the two are related...

andburch commented 6 months ago

I have the same problems, but now they happen even when I don't use "showtext". I get all the cut-out sections of the font filled with black.

tomwenseleers commented 6 months ago

Can you give a reproducible example perhaps?

andburch commented 6 months ago

It's the same as the original post in this thread: https://github.com/tomwenseleers/export/issues/45#issue-1108264533.

I realize now that if I started a new R session that had never run or called showtext, there wouldn't be a font issue. So I guess I was wrong about that part.

But I was having the exact same issues mentioned here, the same as the second reprex too. I managed to fix this issue by using extrafont instead, but otherwise it was the same for me.

R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)