Last active
July 2, 2022 11:42
-
-
Save dmbates/87c56abd816045ceaad2564dbadeefcd to your computer and use it in GitHub Desktop.
Using ggplot2 in IJulia notebook through RCall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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() | |
""" | |
``` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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