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

Ribbon geom examples

ggity_geom_ribbon_test.livemd

Ribbon geom examples

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

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

Basic example

Examples.economics()
|> Plot.new(%{x: "date", y_max: "unemploy"})
|> Plot.geom_ribbon()
|> Plot.labs(title: "Basic Area Chart")
|> Kino.render()

Area example

data = Examples.economics_long()
mask1 = Explorer.Series.equal(data["variable"], "psavert")
mask2 = Explorer.Series.equal(data["variable"], "uempmed")
mask = Explorer.Series.or(mask1, mask2)

data
|> Explorer.DataFrame.mask(mask)
|> Plot.new(%{x: "date", y_max: "value01"})
|> Plot.geom_area(%{fill: "variable"})
|> Plot.scale_fill_viridis(option: :cividis)
|> Plot.scale_y_continuous(labels: fn value -> Float.round(value, 2) end)
|> Plot.labs(title: "Stacked Area Chart")
|> Kino.render()

Complicated ribbon with line example

supp_data =
  Explorer.DataFrame.filter_with(
    Examples.economics_long(),
    &Explorer.Series.equal(&1["variable"], "psavert")
  )

Examples.economics_long()
|> Explorer.DataFrame.to_rows()
|> Enum.filter(fn row -> row["variable"] in ["pop", "pce", "psavert"] end)
|> Enum.with_index(fn row, index ->
  row
  |> Map.put("more", Map.get(row, "value01") + index * 0.0001)
  |> Map.put("less", Map.get(row, "value01") - index * 0.0001)
end)
|> Plot.new(%{x: "date", y_max: "more", y_min: "less", fill: "variable"})
|> Plot.geom_ribbon(alpha: 1)
|> Plot.scale_y_continuous(labels: fn value -> Float.round(value, 2) end)
|> Plot.scale_fill_viridis(option: :plasma)
|> Plot.geom_line(%{x: "date", y: "value01"},
  color: "grey",
  size: 0.5,
  data: supp_data
)
|> Plot.labs(title: "Fancy Ribbons")
|> Kino.render()