Powered by AppSignal & Oban Pro

Data ellipses: stat_ellipse

notebooks/stat_ellipse.livemd

Data ellipses: stat_ellipse

Mix.install([
  {:giggly, path: Path.join(__DIR__, "..")},
  {:kino, "~> 0.19"}
  ])

About this notebook

This notebook follows the examples on ggplot2's reference page for stat_ellipse(), in the order the page gives them. Each example shows ggplot2's code first and then Giggly's. An example Giggly cannot draw yet keeps its ggplot2 code and names what it is waiting for, so the page is also a list of what remains.

stat_ellipse() draws an ellipse around the points of each group, by the method of the car package's dataEllipse(): by default the ellipse at the 95% level of a multivariate t-distribution fitted to x and y, drawn as a path. type = "norm" fits a normal distribution instead, and type = "euclid" draws a circle whose radius is level. Giggly has none of it yet. The examples use R's faithful: how long each eruption of the Old Faithful geyser lasted, and the wait until the next.

An ellipse around the points

ggplot(faithful, aes(waiting, eruptions)) +
  geom_point() +
  stat_ellipse()

Not in Giggly yet: the faithful dataset and stat_ellipse().

An ellipse for each group

ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
  geom_point() +
  stat_ellipse()

eruptions > 3 parts the short eruptions from the long, and colouring by it also groups by it, so each group gets an ellipse of its own.

Not in Giggly yet: the faithful dataset and stat_ellipse().

Normal and t ellipses

ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
  geom_point() +
  stat_ellipse(type = "norm", linetype = 2) +
  stat_ellipse(type = "t")

Not in Giggly yet: the faithful dataset, and stat_ellipse() and its type option.

A circle

ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
  geom_point() +
  stat_ellipse(type = "norm", linetype = 2) +
  stat_ellipse(type = "euclid", level = 3) +
  coord_fixed()

type = "euclid" draws a circle of radius 3 around each group's centre, which looks round only because coord_fixed() gives a unit of x the same length on the page as a unit of y.

Not in Giggly yet: the faithful dataset, stat_ellipse() and its type and level options, and coord_fixed().

Filled ellipses

ggplot(faithful, aes(waiting, eruptions, fill = eruptions > 3)) +
  stat_ellipse(geom = "polygon")

Not in Giggly yet: the faithful dataset, stat_ellipse() drawn as polygons, and geom_polygon().