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

Text Geom Examples

test/visual/ggity_geom_text_test.livemd

Text 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

simple_bar_data =
  Explorer.DataFrame.new([
    %{"salesperson" => "Joe", "week" => "Week 1", "units" => 10},
    %{"salesperson" => "Jane", "week" => "Week 1", "units" => 15},
    %{"salesperson" => "Paul", "week" => "Week 1", "units" => 5},
    %{"salesperson" => "Joe", "week" => "Week 2", "units" => 4},
    %{"salesperson" => "Jane", "week" => "Week 2", "units" => 10},
    %{"salesperson" => "Paul", "week" => "Week 2", "units" => 8},
    %{"salesperson" => "Joe", "week" => "Week 3", "units" => 14},
    %{"salesperson" => "Paul", "week" => "Week 3", "units" => 8},
    %{"salesperson" => "Jane", "week" => "Week 3", "units" => 9},
    %{"salesperson" => "Joe", "week" => "Week 4", "units" => 14},
    %{"salesperson" => "Jane", "week" => "Week 4", "units" => 9}
  ])

Basic example

Examples.mtcars()
|> DataFrame.filter_with(&Series.contains(&1["model"], "Merc"))
|> Plot.new(%{x: :wt, y: :mpg, label: :model})
|> Plot.geom_point()
|> Plot.geom_text(%{alpha: :gear}, color: "blue", nudge_x: 5, hjust: :left, size: 8)
|> Plot.scale_alpha_discrete(guide: :legend)
|> Plot.xlab("Weight (tons)")
|> Plot.ylab("Miles Per Gallon")
|> Kino.render()

Labelled bars

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer"})
|> Plot.geom_bar()
|> Plot.geom_text(%{label: :count},
  position: :dodge,
  family: "Courier New",
  fontface: "bold",
  color: "cornflowerblue",
  stat: :count,
  size: 8,
  nudge_y: 5
)
|> Kino.render()

Stacked bars

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer", group: "class"})
|> Plot.geom_bar(%{fill: "class"}, position: :stack)
|> Plot.geom_text(
  %{label: "count"},
  color: "grey",
  stat: :count,
  position: :stack,
  position_vjust: 0.5,
  fontface: "bold",
  size: 6
)
|> Plot.scale_fill_viridis(option: :inferno)
|> Kino.render()

Column stack

simple_bar_data
|> Plot.new(%{x: "week", y: "units", label: "units", group: "salesperson"})
|> Plot.geom_col(%{fill: "salesperson"}, position: :stack)
|> Plot.geom_text(
  color: "#BAAC6F",
  position: :stack,
  position_vjust: 0.5,
  fontface: "bold",
  size: 6
)
|> Plot.scale_fill_viridis(option: :cividis)
|> Kino.render()

Dodged bars

manufacturer_subset.()
|> Plot.new(%{x: "manufacturer", group: "class"})
|> Plot.geom_bar(%{fill: "class"}, position: :dodge)
|> Plot.geom_text(%{y: "count", label: "count"},
  color: "grey",
  stat: :count,
  position: :dodge,
  position_vjust: 0.5,
  fontface: "bold",
  size: 6
)
|> Plot.scale_fill_viridis(option: :inferno)
|> Kino.render()

Dodged columns

simple_bar_data
|> Plot.new(%{x: "week", y: "units", label: "units", group: "salesperson"})
|> Plot.geom_col(%{fill: "salesperson"}, position: :dodge)
|> Plot.geom_text(
  color: "#BAAC6F",
  position: :dodge,
  fontface: "bold",
  position_vjust: 0.5,
  size: 6
)
|> Plot.scale_fill_viridis(option: :cividis)
|> Kino.render()