Skip to content

Instantly share code, notes, and snippets.

@dmbates
Last active July 2, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmbates/87c56abd816045ceaad2564dbadeefcd to your computer and use it in GitHub Desktop.
Save dmbates/87c56abd816045ceaad2564dbadeefcd to your computer and use it in GitHub Desktop.
Using ggplot2 in IJulia notebook through RCall
---
jupyter: julia-1.7
---
```{julia}
using RCall
```
```{julia}
RCall.ijulia_setdevice(MIME("image/svg+xml"))
```
```{julia}
R"""
#| label: fig-mpghwydispl
#| fig-cap: Highway gas mileage versus engine displacement by vehicle class
library(ggplot2)
ggplot(mpg, aes(displ, hwy, color = class)) + geom_point()
"""
```
---
jupyter: julia-1.7
format:
html:
code-fold: true
---
Load [RCall.jl](https://github.com/JuliaInterop/RCall.jl) and set the R graphics device output to SVG (Scalable Vector Graphics).
```{julia}
using RCall
RCall.ijulia_setdevice(MIME("image/svg+xml"));
```
As seen in @fig-mpghwydispl, cars with engines of lower displacement tend to have better gas mileage.
```{julia}
#| label: fig-mpghwydispl
#| fig-cap: Highway gas mileage versus engine displacement by vehicle class
R"""
library(ggplot2)
print(ggplot(mpg, aes(displ, hwy, color=class)) + geom_point())
""";
```
By explicitly `print`ing the plot in R and suppressing the output printing in Julia (the semicolon after the `@R_str` macro call) the plot and caption are displayed as desired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment