starting visualization journey

This commit is contained in:
behinger (s-ccs 001) 2023-09-25 11:35:13 +00:00
parent 357d1a8b79
commit 16806013a9
9 changed files with 2055 additions and 1 deletions

View File

@ -61,7 +61,7 @@ website:
contents:
- href: material/3_wed/docs/handout.qmd
text: "📝 1 - Docs"
- href: material/3_wed/vis/slides.jl
- href: material/3_wed/vis/handout.qmd
text: "📝 2 - Visualizations"
- href: material/3_wed/linalg/slides.qmd
text: "📝 3 - LinearAlgebra"

21
cheatsheets/ggplotAOG.qmd Normal file
View File

@ -0,0 +1,21 @@
# AlgebraOfGraphics.jl vs. GGPlot.jl
[CC-by-SA Pumas.ai](https://tutorials.pumas.ai/html/PlottingInJulia/01-AoG-intro.html)
|---|---|---|
||ggplot2 |AoG.jl
|Input data |ggplot(df) | data(df)
|Map aesthetics |aes(...) |mapping(...)
|Add geometries |geom_*(...) |visual(...)
|Combine layers | + |*
|Facetting |facet_[wrap|grid](~ column) |mapping(...; [row|col|layout]=:column)
|Customize scales |scale_*_manual() | renamer(...)
|Themes theme_*(...) |set_theme!(theme_*()); |draw(plt)
|Customize axes labels |[x|y]lab("...") |draw(plt, axis=(; [x|y]label="..."))
|Customize color |scale_[fill|color]_*(...) |draw(plt, palettes=(; color=...)) or visual(..., colormap=...")
|Save plot |ggsave("file.[png|svg]") |save("file.[png|svg]", draw(plt))
|Frequency |geom_bar() or stat_count() |frequency()
|Histogram |geom_histogram or stat_bin() |histogram()
|Density |geom_density or stat_density() |density()
|Expectation/Mean| stat_summary(fun = "mean") |expectation()
|Smooth trend |stat_smooth or geom_smooth() |(visual(...) + smooth())
|Linear trend |stat_smooth(method = "lm") or geom_smooth(method = "lm")| (visual(...) + linear())
|Log scale |scale_[x|y]_log10() |draw(plt; axis=(; [x|y]scale=log10))

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
PalmerPenguins = "8b842266-38fa-440a-9b57-31493939ab85"

View File

@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"---\n",
"\n",
"jupyter: julia-1.9\n",
"---"
],
"id": "d0dd3b54"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Makie.jl\n",
"\n",
"## Backends\n",
"Four backends:\n",
"1. `CairoMakie` - SVG\n",
"2. `GLMakie` - 2D/3D/fast interactivity\n",
"3. `WGLMakie` - Same as GLMakie, but in browser\n",
"4. `RPRMakie` - experimental raytracing\n",
"\n",
"I will use `GLMakie` or `CairoMakie`. To switch use `CairoMakie.activate!()`\n",
"\n",
"## Layouts for scientific figures\n",
"\n",
"# Pluto.jl for easy interactivity\n",
"\n",
"# Grammar of Graphics\n",
"\n",
"The grammar of graphics is a convenient way to build common explorative plots.\n",
"\n",
"For example:\n",
"\n",
"\n",
"#### For ggplot enthusiasts:\n",
"You could use [TidierPlots.jl - a ggplot clone](https://github.com/TidierOrg/TidierPlots.jl)\n",
"\n",
"Check out the [../../../../cheatsheets/ggplotAOG.qmd]:\n",
"\n",
"## AlgebraOfGraphics.jl\n",
"\n",
"### Loading data"
],
"id": "0ca52ae6"
},
{
"cell_type": "code",
"metadata": {},
"source": [
"using GLMakie # backend\n",
"using AlgebraOfGraphics\n",
"using PalmerPenguins, DataFrames # example dataset\n",
"\n",
"penguins = dropmissing(DataFrame(PalmerPenguins.load()))\n",
"first(penguins, 6)"
],
"id": "56b4e637",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"::: callout-note\n",
"A `tidy dataframe` is a dataframe that follows these three rules:\n",
"\n",
"\n",
"1. Every column is a variable.\n",
"2. Every row is an observation.\n",
"3. Every cell is a single value.\n",
"\n",
"Tidy data make your visualization life much easier as you will see!\n",
":::\n",
"\n",
"\n",
"### AoG basics\n",
"\n",
"\n",
"`data * mapping * visual`\n",
"\n",
"```julia\n",
" vis_pen = data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * visual(Scatter) \n",
" draw(vis_pen)\n",
"```\n",
"\n",
"### Adding color\n",
"\n",
"```julia\n",
"vis_pencolor = data(penguins) * mapping(:bill_length_mm, :bill_depth_mm, color = :species) * visual(Scatter)\n",
"draw(vis_pencolor)\n",
"\n",
"```\n",
"But that is a bit redundant, you can shortcut this, by reusing existing mappings / inputs:\n",
"```julia\n",
"vis_pencolor2 = vis_pen * mapping(color=:species)\n",
"draw(vis_pencolor2)\n",
"\n",
"```\n",
"\n",
"### Why `Algebra`OfGraphics?\n",
"\n",
"Follows some algebraic rules of multiplying out sums\n",
"\n",
"`data * mapping * visual(Scatter+Lines)`\n",
"\n",
"```julia\n",
"\n",
" vis_pen = data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * visual(Scatter+Lines) \n",
"```\n",
"\n",
"### Faceting\n",
"jl plt = penguin_bill * layers * mapping(color = :species, col = :sex)"
],
"id": "3ead8444"
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.8",
"language": "julia",
"display_name": "Julia 1.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,116 @@
---
jupyter: julia-1.9
---
# Makie.jl
## Backends
Four backends:
1. `CairoMakie` - SVG
2. `GLMakie` - 2D/3D/fast interactivity
3. `WGLMakie` - Same as GLMakie, but in browser
4. `RPRMakie` - experimental raytracing
I will use `GLMakie` or `CairoMakie`. To switch use `CairoMakie.activate!()`
## Layouts for scientific figures
# Pluto.jl for easy interactivity
# Grammar of Graphics
The grammar of graphics is a convenient way to build common explorative plots.
For example:
#### For ggplot enthusiasts:
You could use [TidierPlots.jl - a ggplot clone](https://github.com/TidierOrg/TidierPlots.jl)
Check out the [../../../../cheatsheets/ggplotAOG.qmd]:
## AlgebraOfGraphics.jl
### Loading data
```{julia}
using GLMakie # backend
using AlgebraOfGraphics
using PalmerPenguins, DataFrames # example dataset
penguins = dropmissing(DataFrame(PalmerPenguins.load()))
first(penguins, 6)
```
::: callout-note
A `tidy dataframe` is a dataframe that follows these three rules:
1. Every column is a variable.
2. Every row is an observation.
3. Every cell is a single value.
Tidy data make your visualization life much easier as you will see!
:::
### AoG basics
`data * mapping * visual`
```{julia}
vis_pen = data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * visual(Scatter)
draw(vis_pen)
```
### Adding color
```{julia}
vis_pencolor = data(penguins) * mapping(:bill_length_mm, :bill_depth_mm, color = :species) * visual(Scatter)
draw(vis_pencolor)
```
But that is a bit redundant, you can shortcut this, by reusing existing mappings / inputs:
```{julia}
vis_pencolor2 = vis_pen * mapping(color=:species)
draw(vis_pencolor2)
```
### Why `Algebra`OfGraphics?
Follows some algebraic rules of multiplying out sums
`data * mapping * (visual(Scatter)+visual(Lines))`
```{julia}
data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * (visual(Scatter)+visual(Lines)) |> draw
```
### Faceting
```{julia}
data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * mapping(color = :species, col = :sex) |> draw
```
```{julia}
data(penguins) * mapping(:bill_length_mm, :bill_depth_mm) * mapping(color = :species, col = :sex,row=:body_mass_g => x-> x>3500) |> draw
```
### Linear & Non-linear summaries
```{julia}
data(penguins) * mapping(:bill_length_mm, :bill_depth_mm, color=:species) * (linear() + visual(Scatter)) |> draw
```
```{julia}
data(penguins) * mapping(:bill_length_mm, :bill_depth_mm, color=:species) * (smooth() + visual(Scatter)) |> draw
```
# Interactivity
With Makie.jl, two ways of interactivity:
**Observables** - very general way, a little bit more verbose
**Pluto.jl Sliders** - very simple, need to redraw plot everytime^[it is technically possible t combine Pluto with Observables, but it is a bit buggy]