Quarto - SVG Charts

10/03/2024, Thu
Categories: #Python
Tags: #quarto

Render GGplot in R as SVG Images

When you enlarge the view of a qqplot chart, you would like to see a crisp zoomed in version of the chart. We will achieve this by using svg charts.

For one to use svg charts in ggplot, it requires a download of another package called 'svglite'. However, R dependency manager doesn't quite install sub-dependencies for you, which makes it challenging to get all the dependencies you need for a project. To alleviate this issue, install renv to make managing R dependencies a less complicated ordeal.

The following example assumes that you are on an Ubuntu machine.

Enter the R console by typing

R

and pressing enter on your console.

install.packages("renv")

Exit the R interpreter by typing

quit()

We want to install a systemfont dependency in Ubuntu because the svglite package depends on this. If you are on another operating system, find the equivalent font package.

apt update
apt install libfontconfig1-dev

Go back into the R interpreter and use renv to manage the dependencies of the project.

You use renv similarly to git. Start using renv by initializing the project.

renv::init()

Install the ggplot2 and svglite package

renv::install('ggplot2')
renv::install('svglite')

Update the renv.lock

renv::snapshot()

exit the R interpreter.

Add the following sample code to display a 'plot.svg'

```{r}
library(ggplot2)

p <- ggplot(airquality, aes(Temp, Ozone)) + 
geom_point() + 
geom_smooth(method = "loess")

ggsave("./plot.svg", p, device = "svg")
```

Render the plot in the document.

![my plot](plot.svg)