-2

I made a plot in R with the ggplot package while changing the font of the texts using the following code.

require(ggplot2)
require(extrafont)
x1 <- runif (100,0,1)
x2 <- x1 + runif (100,0,1)

d <- data.frame(x1=x1, x2=x2)
ggplot(d, aes(x1, x2))+geom_point()+
  geom_smooth()+
  ggtitle("") +
  xlab("A") + ylab("Proportion") +
  theme_bw() +
  theme(text=element_text(family="Times New Roman", face="bold", size=12))

Using RStudio, I exported the graph in the PDF format. Then, I imported the graph in my word document. However, the texts did not show up in the graph when I open the document with Microsoft Word. This does not happen when I do not change the font to Times New Roman.

I would appreciate it if I could hear any suggestions. Thank you.

user8460166
  • 73
  • 1
  • 6
  • 24
  • 1
    [Reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example please... – Conor Neilson Nov 18 '18 at 20:24
  • Can you post sample data? Please edit **the question** with the output of `dput(dat2)`. Or, if it is too big with the output of `dput(head(dat2, 20))`. – Rui Barradas Nov 18 '18 at 20:24
  • Thank you for the suggestion. I edited to provide my reproducible example. – user8460166 Nov 18 '18 at 22:52
  • 1
    Please see here for how to register your fonts with your PDF device, which might be the problem here. https://cran.r-project.org/web/packages/extrafont/README.html You might also consider using the `officer` package and/or related `rvg` package to export vector graphics for use and editing in Word. – Jon Spring Nov 18 '18 at 23:50
  • Thank you for your suggestion, @JonSpring! I was able to solve my problem by looking at the link you provided. I appreciate it! – user8460166 Nov 20 '18 at 21:44

1 Answers1

1

Thanks to many helpful comments and especially the link provided by Jon Spring, I was able to solve my problem. I would like to write down the code here for someone who gets here due to the similar problem.

The approach I took was to embed fonts into PDFs by using the extrafont package's embedFonts function.

require(ggplot2)
require(extrafont)
x1 <- runif (100,0,1)
x2 <- x1 + runif (100,0,1)
d <- data.frame(x1=x1, x2=x2)

pdf("a.pdf")

ggplot(d, aes(x1, x2))+geom_point()+
          geom_smooth()+
          ggtitle("") +
          xlab("A") + ylab("Proportion") +
          theme_bw() +
          theme(text=element_text(family="Times New Roman", face="bold", size=12))

dev.off()

embedFonts(file = "a.pdf")
user8460166
  • 73
  • 1
  • 6
  • 24