I'm trying to translate some of my R code to Julia. I mainly struggle with the difference in plotting as I'm very used to ggplot2.
There I can do:
mpg %>%
ggplot(aes(x = displ, y = cyl, color = manufacturer)) +
geom_point() +
facet_wrap(~class)
With this I get subplots, consistent x- and y-axes, a shared legend, and much more. How would I achieve this with Julia, say the GR backend?
Update: The mpg
data set looks like:
# A tibble: 234 × 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
4 audi a4 2 2008 4 auto(av) f 21 30 p compact
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
7 audi a4 3.1 2008 6 auto(av) f 18 27 p compact
8 audi a4 quattro 1.8 1999 4 manual(m5) 4 18 26 p compact
9 audi a4 quattro 1.8 1999 4 auto(l5) 4 16 25 p compact
10 audi a4 quattro 2 2008 4 manual(m6) 4 20 28 p compact
# … with 224 more rows
The easiest way to do this at present might be with Makie.jl, which gives you very granular control over the plotting process. For example:
# using GLMakie # If you want an interactive plot window and raster graphics
# or
using CairoMakie # If you want to save vector graphics
fig = Figure()
ax1 = Axis(fig[1, 1])
ax2 = Axis(fig[1, 2])
ax3 = Axis(fig[2, 1:2])
l1 = lines!(ax1, 0..10, sin, color=:red)
l2 = lines!(ax2, 0..10, cos, color=:blue)
l3 = lines!(ax3, 0..10, sqrt, color=:green)
ax1.ylabel = "amplitude"
ax3.ylabel = "amplitude"
ax3.xlabel = "time"
Legend(fig[1:2, 3], [l1, l2, l3], ["sin", "cos", "sqrt"])
fig
and optionally
save("filename.ext", fig)
There are a number of good examples and tutorials in https://makie.juliaplots.org/v0.15.1/tutorials/basic-tutorial/ and https://lazarusa.github.io/BeautifulMakie/
If you tend to like the "grammar of graphics" style, you might also check out AlgebraOfGraphics.jl, which is built on top of Makie, or (though I haven't tried it in some time) Gadfly.jl, which was actually one of the first plotting packages in Julia.