How to set SVG as default rendering in RMarkdown?

Viewed 1042

When using JupyterLab, there is a command %config InlineBackend.figure_format = 'svg' that displays plots as svg. The quality is awesome. I would like to ask if there is a similar command for Rnotebook, i.e.

  • Display plots inside Rnotebook as svg.

  • Display plots in the output html as svg.

1 Answers

For every code chunk that you want to produce svg output, you simply set the dev = 'svg' chunk option. Or you can set this option globally with knitr::opts_chunk$set(dev = 'svg') at the beginning of your markdown document.

Save the following as a .Rmd file, knit, and see what you get.

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = 'svg') # set output device to svg
```

A simple plot, as svg file:

```{r}
library(ggplot2)

ggplot(mtcars, aes(mpg, disp, color = hp)) +
  geom_point() +
  scale_color_viridis_c()
```
Related