6. Data Visualization#
Adapted from https://ucla-biostat-257.github.io/2023spring/slides/04-juliaplot/juliaplots.html
Let’s load some data into Julia:
# Pkg.add("RDatasets")
using RDatasets, DataFrames
df0 = RDatasets.dataset("datasets", "ToothGrowth")
describe(df0)
3×7 DataFrame
Row | variable | mean | min | median | max | nmissing | eltype |
---|---|---|---|---|---|---|---|
Symbol | Union… | Any | Union… | Any | Int64 | DataType | |
1 | Len | 18.8133 | 4.2 | 19.25 | 33.9 | 0 | Float64 |
2 | Supp | OJ | VC | 0 | CategoricalValue{String, UInt8} | ||
3 | Dose | 1.16667 | 0.5 | 1.0 | 2.0 | 0 | Float64 |
Now let’s summarize that data, grouping by Supp
and Dose
:
using DataFramesMeta, Statistics
df =
@chain df0 begin
groupby([:Supp, :Dose])
@combine begin
:se = std(:Len) / length(:Len)
:len = mean(:Len)
:n = length(:Len)
end
end
df
6×5 DataFrame
Row | Supp | Dose | se | len | n |
---|---|---|---|---|---|
Cat… | Float64 | Float64 | Float64 | Int64 | |
1 | VC | 0.5 | 0.274663 | 7.98 | 10 |
2 | VC | 1.0 | 0.251531 | 16.77 | 10 |
3 | VC | 2.0 | 0.479773 | 26.14 | 10 |
4 | OJ | 0.5 | 0.445971 | 13.23 | 10 |
5 | OJ | 1.0 | 0.391095 | 22.7 | 10 |
6 | OJ | 2.0 | 0.265506 | 26.06 | 10 |
6.1. Gadfly.jl#
Gadfly.jl
is like ggplot2
in R:
using Gadfly
df[!, :ymin] = df[!, :len] - df[!, :se]
df[!, :ymax] = df[!, :len] + df[!, :se]
Gadfly.plot(
df,
x = :Dose,
y = :len,
color = :Supp,
Geom.point,
Guide.xlabel("Dose"),
Guide.ylabel("Tooth Length"),
Guide.xticks(ticks = [0.5, 1.0, 1.5, 2.0]),
Geom.line,
Geom.errorbar,
ymin = :ymin,
ymax = :ymax,
Scale.color_discrete_manual("orange", "skyblue"))
Gadfly provides many customization options:
Gadfly.plot(
df,
x = :Dose,
y = :len,
color = :Supp,
Geom.point,
Guide.xlabel("Dose"),
Guide.ylabel("Tooth Length"),
Guide.xticks(ticks = [0.5, 1.0, 1.5, 2.0]),
Theme(
panel_fill = nothing,
highlight_width = 0mm,
point_size = 0.5mm,
key_position = :inside,
grid_line_width = 0mm,
panel_stroke = "black"),
Geom.line,
Geom.errorbar,
ymin = :ymin,
ymax = :ymax,
Scale.color_discrete_manual("orange", "skyblue"))
More on Gadfly
: [https://gadflyjl.org/stable/]
6.2. Plots.jl#
using Plots, Random
Random.seed!(123) # set seed
x = cumsum(randn(50, 2), dims=1);
6.2.1. Plots.jl using PyPlot
backend#
# Pkg.add("PyPlot")
using PyPlot
pyplot() # set the backend to PyPlot
Plots.plot(x, title="Random walk", xlab="time")
6.2.2. Plots.jl using GR
backend#
gr() # change backend to GR
Plots.plot(x, title="Random walk", xlab="time")
6.2.3. Plotly backend#
# Pkg.add("PlotlyJS")
using PlotlyJS
plotlyjs() # change backend to PlotlyJS
Plots.plot(x, title="Random walk", xlab="time")
The WebIO Jupyter extension was not detected. See the WebIO Jupyter integration documentation for more information.