Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Bar Geom Examples

test/visual/ggity_geom_bar_test.livemd

Bar Geom Examples

Mix.install([
  {:ggity, path: ".", override: true},
  {:kino_ggity, github: "srowley/kino_ggity"}
])

alias Explorer.{DataFrame, Series}
alias GGity.{Examples, Kino, Plot}

manufacturer_subset = fn ->
  manufacturers = Explorer.Series.from_list(["chevrolet", "audi", "ford", "nissan", "subaru"])

  Explorer.DataFrame.filter_with(
    Examples.mpg(),
    &Explorer.Series.in(&1["manufacturer"], manufacturers)
  )
end

Basic bar chart

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer"})
|> Plot.geom_bar(
  custom_attributes: fn plot, row ->
    [onclick: "alert('#{plot.labels.y}: #{row["count"]}')"]
  end
)
|> Plot.scale_y_continuous(labels: &floor/1)
|> Kino.render()

Stacked bar chart

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer"})
|> Plot.geom_bar(
  %{fill: "class"},
  custom_attributes: fn plot, row ->
    [onclick: "alert('#{plot.labels.y}: #{row["count"]}')"]
  end
)
|> Plot.scale_fill_viridis(option: :inferno)
|> Kino.render()

Dodge adjustment

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer"})
|> Plot.geom_bar(%{fill: "class"}, position: :dodge)
|> Kino.render()

geom_col example

[
  %{"salesperson" => "Joe", "week" => "Week 1", "units" => 10},
  %{"salesperson" => "Jane", "week" => "Week 1", "units" => 15},
  %{"salesperson" => "Joe", "week" => "Week 2", "units" => 4},
  %{"salesperson" => "Jane", "week" => "Week 2", "units" => 10},
  %{"salesperson" => "Joe", "week" => "Week 3", "units" => 14},
  %{"salesperson" => "Jane", "week" => "Week 3", "units" => 9}
]
|> Explorer.DataFrame.new()
|> Plot.new(%{x: "week", y: "units", fill: "salesperson"})
|> Plot.geom_col(position: :dodge, alpha: 0.7)
|> Plot.scale_fill_viridis(option: :cividis)
|> Kino.render()