子图中的共享图例

Shared legend in subplots

我正在尝试将我的一些 R 代码翻译成 Julia。我主要为绘图的差异而苦恼,因为我已经非常习惯 ggplot2。

我能做的:

mpg %>% 
ggplot(aes(x = displ, y = cyl, color = manufacturer)) + 
geom_point() + 
facet_wrap(~class)

有了这个,我得到了子图、一致的 x 轴和 y 轴、共享的图例等等。 我将如何使用 Julia 实现这一点,比如 GR 后端?

更新:mpg 数据集如下所示:

# 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

我得到的输出类似于:

目前执行此操作的最简单方法可能是使用 Makie.jl,这使您可以非常精细地控制绘图过程。例如:

# 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

和可选的

save("filename.ext", fig)

产量

https://makie.juliaplots.org/v0.15.1/tutorials/basic-tutorial/ 中有许多很好的示例和教程 和 https://lazarusa.github.io/BeautifulMakie/

如果您倾向于喜欢“图形语法”风格,您也可以查看 AlgebraOfGraphics.jl, which is built on top of Makie, or (though I haven't tried it in some time) Gadfly.jl,它实际上是 Julia 中最早的绘图包之一。