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

GGity Examples

livebooks/geom_point.livemd

GGity Examples

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

alias GGity.{Examples, Kino, Plot}

livebook_margins = %{left: 70, top: 25, right: 70, bottom: 25}

geom_boxplot()

Examples.mpg()
|> Plot.new(%{x: "class", y: "hwy"}, margins: livebook_margins)
|> Plot.geom_boxplot()
|> Kino.render()
Examples.mpg()
|> Plot.new(%{x: "class", y: "hwy"}, margins: livebook_margins)
|> Plot.geom_boxplot(fill: "pink", color: "blue")
|> Kino.render()
Examples.mpg()
|> Plot.new(%{x: "class", y: "hwy"}, margins: livebook_margins)
|> Plot.geom_boxplot(outlier_color: "red", outlier_shape: 1)
|> Kino.render()
Examples.mpg()
|> Plot.new(%{x: "class", y: "hwy"}, margins: livebook_margins)
|> Plot.geom_boxplot(outlier_shape: :na)
|> Plot.geom_point(size: 3)
|> Kino.render()
Examples.mpg()
|> Plot.new(%{x: "class", y: "hwy", color: "drv"}, margins: livebook_margins)
|> Plot.geom_boxplot()
|> Kino.render()

geom_point()

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Kino.render()

Add aesthetic mappings to color:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{color: :cyl})
|> Kino.render()

Add an aesthetic mapping to shape:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{shape: :cyl})
|> Kino.render()

Add aesthetic mapping to size (for circles, a bubble chart)

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{size: :qsec})
|> Kino.render()

Set aesthetics to fixed value

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(color: "red", size: 5)
|> Kino.render()

geom_line()

Examples.economics()
|> Plot.new(%{x: "date", y: "unemploy"}, margins: livebook_margins)
|> Plot.geom_line()
|> Kino.render()
Examples.economics_long()
|> Plot.new(%{x: "date", y: "value01", color: "variable"}, margins: livebook_margins)
|> Plot.geom_line()
|> Kino.render()
Examples.economics()
|> Plot.new(%{x: "date", y: "unemploy"}, margins: livebook_margins)
|> Plot.geom_line(color: "red")
|> Kino.render()

geom_text()

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_text()
|> Kino.render()

Set the font size for the label:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_text(size: 5)
|> Kino.render()

Shift positioning:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_point(size: 2)
|> Plot.geom_text(size: 5, hjust: :left, nudge_x: 3)
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_point(size: 2)
|> Plot.geom_text(size: 5, vjust: :top, nudge_y: 3)
|> Kino.render()

Map other aesthetics:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_text(%{color: :cyl}, size: 5)
|> Kino.render()

Add a text annotation:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg, label: :model}, margins: livebook_margins)
|> Plot.geom_text(size: 5)
|> Plot.annotate(:text, label: "plot mpg vs. wt", x: 1.5, y: 15, size: 8, color: "red")
|> Kino.render()

scale_color_viridis()

The viridis scale is the default color scale.

Examples.diamonds()
|> Explorer.DataFrame.sample(1000, seed: 100)
|> Plot.new(%{x: "carat", y: "price"}, margins: livebook_margins)
|> Plot.geom_point(%{color: "clarity"})
|> Kino.render()

Use the :option option to select a palette:

cities = ["Houston", "Fort Worth", "San Antonio", "Dallas", "Austin"]

Examples.tx_housing()
|> Explorer.DataFrame.filter_with(&Explorer.Series.in(&1["city"], cities))
|> Plot.new(%{x: "sales", y: "median"}, margins: livebook_margins)
|> Plot.geom_point(%{color: "city"})
|> Plot.scale_color_viridis(option: :plasma)
|> Kino.render()
Examples.tx_housing()
|> Explorer.DataFrame.filter_with(&Explorer.Series.in(&1["city"], cities))
|> Plot.new(%{x: "sales", y: "median"}, margins: livebook_margins)
|> Plot.geom_point(%{color: "city"})
|> Plot.scale_color_viridis(option: :inferno)
|> Kino.render()

theme()

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Kino.render()
import GGity.Element.{Line, Rect, Text}

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(plot_title: element_text(size: 6))
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(plot_background: element_rect(fill: "green"))
|> Kino.render()

Panel formatting:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(panel_background: element_rect(fill: "white", color: "grey"))
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(panel_grid_major: element_line(color: "black"))
|> Kino.render()

Axis formatting:

# Axis formatting
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(axis_line: element_line(size: 6, color: "grey"))
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(axis_text: element_text(color: "blue"))
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(axis_ticks: element_line(size: 4))
|> Kino.render()

Turn the x-axis ticks inward:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.labs(title: "Fuel economy declines as weight decreases")
|> Plot.theme(axis_ticks_length_x: -2)
|> Kino.render()

GGity does not support legend position, but legend key boxes and text can be styled as you would expect.

Default styling:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{color: :cyl, shape: :vs})
|> Plot.labs(
  x: "Weight (1000 lbs)",
  y: "Fuel economy (mpg)",
  color: "Cylinders",
  shape: "Transmission"
)
|> Kino.render()

Style the legend keys:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{color: :cyl, shape: :vs})
|> Plot.labs(
  x: "Weight (1000 lbs)",
  y: "Fuel economy (mpg)",
  color: "Cylinders",
  shape: "Transmission"
)
|> Plot.theme(legend_key: element_rect(fill: "white", color: "black"))
|> Kino.render()

Style the legend text:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{color: :cyl, shape: :vs})
|> Plot.labs(
  x: "Weight (1000 lbs)",
  y: "Fuel economy (mpg)",
  color: "Cylinders",
  shape: "Transmission"
)
|> Plot.theme(legend_text: element_text(size: 5, color: "red"))
|> Kino.render()

Style the legend title:

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point(%{color: :cyl, shape: :vs})
|> Plot.labs(
  x: "Weight (1000 lbs)",
  y: "Fuel economy (mpg)",
  color: "Cylinders",
  shape: "Transmission"
)
|> Plot.theme(legend_title: element_text(face: "bold"))
|> Kino.render()

annotate()

Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.annotate(:text, x: 4, y: 25, label: "Some text")
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.annotate(:rect, xmin: 3, xmax: 4.2, ymin: 12, ymax: 21, alpha: 0.2)
|> Kino.render()
Examples.mtcars()
|> Plot.new(%{x: :wt, y: :mpg}, margins: livebook_margins)
|> Plot.geom_point()
|> Plot.annotate(:segment, x: 2.5, xend: 4, y: 15, yend: 25, color: "blue")
|> Kino.render()