Powered by AppSignal & Oban Pro

Contours: geom_contour

notebooks/geom_contour.livemd

Contours: geom_contour

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

About this notebook

This notebook follows the examples on ggplot2's reference page for geom_contour(), which covers geom_contour_filled(), stat_contour() and stat_contour_filled() too, 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.

ggplot2 cannot draw a surface in three dimensions, but these draw one in two: geom_contour() draws lines along which the height, z, is constant, and geom_contour_filled() fills the bands between them; stat_contour() and stat_contour_filled() compute them. They need x and y on an evenly spaced grid, each combination once, so irregular data must first be interpolated onto one. Giggly has none of this yet.

Contour lines

# Basic plot
v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
v + geom_contour()

Not in Giggly yet: geom_contour(), mapping z, and the faithfuld dataset.

Contours of raw data

# Or compute from raw data
ggplot(faithful, aes(waiting, eruptions)) +
  geom_density_2d()

geom_density_2d() estimates the density of the points on a grid before drawing its contours. Not in Giggly yet: geom_density_2d(), and the faithful dataset.

Filled contours

# use geom_contour_filled() for filled contours
v + geom_contour_filled()

Not in Giggly yet: geom_contour_filled(), mapping z, and the faithfuld dataset.

The number of contours

# Setting bins creates evenly spaced contours in the range of the data
v + geom_contour(bins = 3)
v + geom_contour(bins = 5)

Not in Giggly yet: geom_contour() and its bins option, mapping z, and the faithfuld dataset.

The distance between contours

# Setting binwidth does the same thing, parameterised by the distance
# between contours
v + geom_contour(binwidth = 0.01)
v + geom_contour(binwidth = 0.001)

Not in Giggly yet: geom_contour() and its binwidth option, mapping z, and the faithfuld dataset.

Coloured contours

# Other parameters
v + geom_contour(aes(colour = after_stat(level)))
v + geom_contour(colour = "red")

Not in Giggly yet: geom_contour(), mapping z, and the faithfuld dataset.

Contours over a raster

v + geom_raster(aes(fill = density)) +
  geom_contour(colour = "white")

Not in Giggly yet: geom_raster(), geom_contour(), mapping z, and the faithfuld dataset.