statquant
statquant

Reputation: 14400

How can I set default font for base graphics in rmarkdown?

I wanted to set the default font for base plots in a rmarkdown document and I realized it is not working. Do anyone know how I can have default font for base graphics in rmarkdown (html)

---                                                                                
title: "Setting font in par"                                                       
output: html_document                                                              
---                                                                                

The way of setting default font for base graphics is to use par
                                                                             
```{r}                                                                             
par(family = "monospace")                                                          
```                                                                                
                                                                                   
Setting the default does NOT work in Rmd while executing directly in terminal does
                                                                                   
```{r}                                                                             
plot(1:10, main = "title") # output of rmarkdown::render does not use the right font
                           # but execution in a terminal does                         
```                                                                                
                                                                                   
Specifying it explicitely does work                                                
                                                                                   
```{r}                                                                             
plot(1:10, main = "title", family = "monospace")                                   
```                                                                                

Upvotes: 1

Views: 69

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30194

Each code chunk is evaluated inside a separate graphics device, which means settings like par() in a previous chunk will not be passed to the next chunk. If you want these settings to be persistent, you need to set the option:

knitr::opts_knit$set(global.device = TRUE)

See this section in R Markdown Cookbook for more info.

Upvotes: 1

Related Questions