Data analysis getting started
Mix.install([
{:explorer, "~> 0.2.0"},
{:kino, "~> 0.6.1"},
{:kino_vega_lite, "~> 0.1.1"}
])
:ok
Section
alias Explorer.{DataFrame, Series}
mpg_file = "~/study/mpg.csv"
mpg =
DataFrame.from_csv!(mpg_file)
|> DataFrame.select([""], :drop)
#Explorer.DataFrame<
Polars[234 x 11]
manufacturer string ["audi", "audi", "audi", "audi", "audi", ...]
model string ["a4", "a4", "a4", "a4", "a4", ...]
displ float [1.8, 1.8, 2.0, 2.0, 2.8, ...]
year integer [1999, 1999, 2008, 2008, 1999, ...]
cyl integer [4, 4, 4, 4, 6, ...]
trans string ["auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto(l5)", ...]
drv string ["f", "f", "f", "f", "f", ...]
cty integer [18, 21, 20, 21, 16, ...]
hwy integer [29, 29, 31, 30, 26, ...]
fl string ["p", "p", "p", "p", "p", ...]
class string ["compact", "compact", "compact", "compact", "compact", ...]
>
VegaLite.new()
|> VegaLite.data_from_values(mpg, only: ["displ", "hwy"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "displ", type: :quantitative)
|> VegaLite.encode_field(:y, "hwy", type: :quantitative)
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"displ":1.8,"hwy":29},{"displ":1.8,"hwy":29},{"displ":2.0,"hwy":31},{"displ":2.0,"hwy":30},{"displ":2.8,"hwy":26},{"displ":2.8,"hwy":26},{"displ":3.1,"hwy":27},{"displ":1.8,"hwy":26},{"displ":1.8,"hwy":25},{"displ":2.0,"hwy":28},{"displ":2.0,"hwy":27},{"displ":2.8,"hwy":25},{"displ":2.8,"hwy":25},{"displ":3.1,"hwy":25},{"displ":3.1,"hwy":25},{"displ":2.8,"hwy":24},{"displ":3.1,"hwy":25},{"displ":4.2,"hwy":23},{"displ":5.3,"hwy":20},{"displ":5.3,"hwy":15},{"displ":5.3,"hwy":20},{"displ":5.7,"hwy":17},{"displ":6.0,"hwy":17},{"displ":5.7,"hwy":26},{"displ":5.7,"hwy":23},{"displ":6.2,"hwy":26},{"displ":6.2,"hwy":25},{"displ":7.0,"hwy":24},{"displ":5.3,"hwy":19},{"displ":5.3,"hwy":14},{"displ":5.7,"hwy":15},{"displ":6.5,"hwy":17},{"displ":2.4,"hwy":27},{"displ":2.4,"hwy":30},{"displ":3.1,"hwy":26},{"displ":3.5,"hwy":29},{"displ":3.6,"hwy":26},{"displ":2.4,"hwy":24},{"displ":3.0,"hwy":24},{"displ":3.3,"hwy":22},{"displ":3.3,"hwy":22},{"displ":3.3,"hwy":24},{"displ":3.3,"hwy":24},{"displ":3.3,"hwy":17},{"displ":3.8,"hwy":22},{"displ":3.8,"hwy":21},{"displ":3.8,"hwy":23},{"displ":4.0,"hwy":23},{"displ":3.7,"hwy":19},{"displ":3.7,"hwy":18},{"displ":3.9,"hwy":17},{"displ":3.9,"hwy":17},{"displ":4.7,"hwy":19},{"displ":4.7,"hwy":19},{"displ":4.7,"hwy":12},{"displ":5.2,"hwy":17},{"displ":5.2,"hwy":15},{"displ":3.9,"hwy":17},{"displ":4.7,"hwy":17},{"displ":4.7,"hwy":12},{"displ":4.7,"hwy":17},{"displ":5.2,"hwy":16},{"displ":5.7,"hwy":18},{"displ":5.9,"hwy":15},{"displ":4.7,"hwy":16},{"displ":4.7,"hwy":12},{"displ":4.7,"hwy":17},{"displ":4.7,"hwy":17},{"displ":4.7,"hwy":16},{"displ":4.7,"hwy":12},{"displ":5.2,"hwy":15},{"displ":5.2,"hwy":16},{"displ":5.7,"hwy":17},{"displ":5.9,"hwy":15},{"displ":4.6,"hwy":17},{"displ":5.4,"hwy":17},{"displ":5.4,"hwy":18},{"displ":4.0,"hwy":17},{"displ":4.0,"hwy":19},{"displ":4.0,"hwy":17},{"displ":4.0,"hwy":19},{"displ":4.6,"hwy":19},{"displ":5.0,"hwy":17},{"displ":4.2,"hwy":17},{"displ":4.2,"hwy":17},{"displ":4.6,"hwy":16},{"displ":4.6,"hwy":16},{"displ":4.6,"hwy":17},{"displ":5.4,"hwy":15},{"displ":5.4,"hwy":17},{"displ":3.8,"hwy":26},{"displ":3.8,"hwy":25},{"displ":4.0,"hwy":26},{"displ":4.0,"hwy":24},{"displ":4.6,"hwy":21},{"displ":4.6,"hwy":22},{"displ":4.6,"hwy":23},{"displ":4.6,"hwy":22},{"displ":5.4,"hwy":20},{"displ":1.6,"hwy":33},{"displ":1.6,"hwy":32},{"displ":1.6,"hwy":32},{"displ":1.6,"hwy":29},{"displ":1.6,"hwy":32},{"displ":1.8,"hwy":34},{"displ":1.8,"hwy":36},{"displ":1.8,"hwy":36},{"displ":2.0,"hwy":29},{"displ":2.4,"hwy":26},{"displ":2.4,"hwy":27},{"displ":2.4,"hwy":30},{"displ":2.4,"hwy":31},{"displ":2.5,"hwy":26},{"displ":2.5,"hwy":26},{"displ":3.3,"hwy":28},{"displ":2.0,"hwy":26},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":28},{"displ":2.0,"hwy":27},{"displ":2.7,"hwy":24},{"displ":2.7,"hwy":24},{"displ":2.7,"hwy":24},{"displ":3.0,"hwy":22},{"displ":3.7,"hwy":19},{"displ":4.0,"hwy":20},{"displ":4.7,"hwy":17},{"displ":4.7,"hwy":12},{"displ":4.7,"hwy":19},{"displ":5.7,"hwy":18},{"displ":6.1,"hwy":14},{"displ":4.0,"hwy":15},{"displ":4.2,"hwy":18},{"displ":4.4,"hwy":18},{"displ":4.6,"hwy":15},{"displ":5.4,"hwy":17},{"displ":5.4,"hwy":16},{"displ":5.4,"hwy":18},{"displ":4.0,"hwy":17},{"displ":4.0,"hwy":19},{"displ":4.6,"hwy":19},{"displ":5.0,"hwy":17},{"displ":2.4,"hwy":29},{"displ":2.4,"hwy":27},{"displ":2.5,"hwy":31},{"displ":2.5,"hwy":32},{"displ":3.5,"hwy":27},{"displ":3.5,"hwy":26},{"displ":3.0,"hwy":26},{"displ":3.0,"hwy":25},{"displ":3.5,"hwy":25},{"displ":3.3,"hwy":17},{"displ":3.3,"hwy":17},{"displ":4.0,"hwy":20},{"displ":5.6,"hwy":18},{"displ":3.1,"hwy":26},{"displ":3.8,"hwy":26},{"displ":3.8,"hwy":27},{"displ":3.8,"hwy":28},{"displ":5.3,"hwy":25},{"displ":2.5,"hwy":25},{"displ":2.5,"hwy":24},{"displ":2.5,"hwy":27},{"displ":2.5,"hwy":25},{"displ":2.5,"hwy":26},{"displ":2.5,"hwy":23},{"displ":2.2,"hwy":26},{"displ":2.2,"hwy":26},{"displ":2.5,"hwy":26},{"displ":2.5,"hwy":26},{"displ":2.5,"hwy":25},{"displ":2.5,"hwy":27},{"displ":2.5,"hwy":25},{"displ":2.5,"hwy":27},{"displ":2.7,"hwy":20},{"displ":2.7,"hwy":20},{"displ":3.4,"hwy":19},{"displ":3.4,"hwy":17},{"displ":4.0,"hwy":20},{"displ":4.7,"hwy":17},{"displ":2.2,"hwy":29},{"displ":2.2,"hwy":27},{"displ":2.4,"hwy":31},{"displ":2.4,"hwy":31},{"displ":3.0,"hwy":26},{"displ":3.0,"hwy":26},{"displ":3.5,"hwy":28},{"displ":2.2,"hwy":27},{"displ":2.2,"hwy":29},{"displ":2.4,"hwy":31},{"displ":2.4,"hwy":31},{"displ":3.0,"hwy":26},{"displ":3.0,"hwy":26},{"displ":3.3,"hwy":27},{"displ":1.8,"hwy":30},{"displ":1.8,"hwy":33},{"displ":1.8,"hwy":35},{"displ":1.8,"hwy":37},{"displ":1.8,"hwy":35},{"displ":4.7,"hwy":15},{"displ":5.7,"hwy":18},{"displ":2.7,"hwy":20},{"displ":2.7,"hwy":20},{"displ":2.7,"hwy":22},{"displ":3.4,"hwy":17},{"displ":3.4,"hwy":19},{"displ":4.0,"hwy":18},{"displ":4.0,"hwy":20},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":26},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":29},{"displ":2.8,"hwy":24},{"displ":1.9,"hwy":44},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":26},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":29},{"displ":2.5,"hwy":29},{"displ":2.5,"hwy":29},{"displ":2.8,"hwy":23},{"displ":2.8,"hwy":24},{"displ":1.9,"hwy":44},{"displ":1.9,"hwy":41},{"displ":2.0,"hwy":29},{"displ":2.0,"hwy":26},{"displ":2.5,"hwy":28},{"displ":2.5,"hwy":29},{"displ":1.8,"hwy":29},{"displ":1.8,"hwy":29},{"displ":2.0,"hwy":28},{"displ":2.0,"hwy":29},{"displ":2.8,"hwy":26},{"displ":2.8,"hwy":26},{"displ":3.6,"hwy":26}]},"encoding":{"x":{"field":"displ","type":"quantitative"},"y":{"field":"hwy","type":"quantitative"}},"mark":"point"}
alias VegaLite, as: Vl
mpg_url = "https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/mpg.csv"
Vl.new()
|> Vl.data_from_url(mpg_url)
|> Vl.mark(:point)
|> Vl.encode_field(:x, "displ", type: :quantitative)
|> Vl.encode_field(:y, "hwy", type: :quantitative)
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"url":"https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/mpg.csv"},"encoding":{"x":{"field":"displ","type":"quantitative"},"y":{"field":"hwy","type":"quantitative"}},"mark":"point"}