How to Make ggsave Use TikZ Graphics Device

I have the following ggplot2 R code chunk:

```{r, dev = "tikz"}
my_graph <- ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color = Species)) +
  xlab("Petal length (cm)") + ylab("Sepal length") +
  ggtitle("Petal length vs. sepal length") +
  geom_point() + theme_bw()
my_graph
```

By adding the chunk option dev = "tikz", I was able to make the plot use the same font as my LaTeX font (which is Latin Modern by default). I can save the plot to a separate PDF:

ggsave(my_graph, filename = "petal-length-vs-sepal-length.pdf")

But the problem is that the output used a different font than the one I want. I want it to use my document font, Latin Modern. Computer Modern is also fine. I tried adding the argument device = "tikz", but that is an unknown TikZ device. According to the ggplot2 documentation, the device tikz is not supported by the ggsave() function. My next option is to set the font in the theme:

my_graph <- ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color = Species)) +
  xlab("Petal length (cm)") + ylab("Sepal length") +
  ggtitle("Petal length vs. sepal length") +
  geom_point() + theme_bw(base_family = "Latin Modern")

But the Latin Modern or the Computer Modern fonts are not available. I can find the available fonts by running names(pdfFonts()):

 [1] "serif"                "sans"                 "mono"                
 [4] "AvantGarde"           "Bookman"              "Courier"             
 [7] "Helvetica"            "Helvetica-Narrow"     "NewCenturySchoolbook"
[10] "Palatino"             "Times"                "URWGothic"           
[13] "URWBookman"           "NimbusMon"            "NimbusSan"           
[16] "URWHelvetica"         "NimbusSanCond"        "CenturySch"          
[19] "URWPalladio"          "NimbusRom"            "URWTimes"            
[22] "ArialMT"              "Japan1"               "Japan1HeiMin"        
[25] "Japan1GothicBBB"      "Japan1Ryumin"         "Korea1"              
[28] "Korea1deb"            "CNS1"                 "GB1"                 

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.