Cartesian coordinates: coord_cartesian
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
alias Giggly.Plot
mtcars = Giggly.Datasets.mtcars()
render = fn plot ->
plot
|> Giggly.to_svg()
|> IO.iodata_to_binary()
|> Kino.Image.new(:svg)
end
About this notebook
This notebook follows the examples on ggplot2's reference page for
coord_cartesian(), 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.
Cartesian coordinates are the familiar kind, and ggplot2's default: x runs across the panel and
y up it. Setting limits on coord_cartesian() zooms the drawing, like looking at it through a
magnifying glass, and leaves the data alone, where setting limits on a scale drops the values
outside them. Its other options turn off the expansion around the data, reverse an axis, fix the
aspect ratio and stop the panel clipping what is drawn. Every Giggly plot is drawn in Cartesian
coordinates unless Giggly.Plot.coord_radial/1 is added, but Giggly takes none of
coord_cartesian()'s options yet. A plot is described with Giggly.Plot and drawn with
Giggly.to_svg/2, which returns an SVG document. The setup cell aliases Giggly.Plot, so the
cells write Plot.new/2, and defines render, which draws a plot and shows it below the cell with
Kino.Image. It binds Giggly.Datasets.mtcars/0, R's mtcars, to mtcars, R's name for it.
The plot to zoom
# There are two ways of zooming the plot display: with scales or
# with coordinate systems. They work in two rather different ways.
p <- ggplot(mtcars, aes(disp, wt)) +
geom_point() +
geom_smooth()
p
mtcars has 32 rows, so the smooth is a loess fit. Giggly fits a loess curve at each of its
points directly, as R's loess(surface = "direct") does, where R's default joins fits made at
fewer places, so its curve sits up to 2.3 points from R's here, and its band's edges up to 7.1.
p =
mtcars
|> Plot.new(%{x: :disp, y: :wt})
|> Plot.geom_point()
|> Plot.geom_smooth()
render.(p)
Limits on a scale
# Setting the limits on a scale converts all values outside the range to NA.
p + scale_x_continuous(limits = c(325, 500))
The limits drop the 24 cars outside them, so the smooth is fitted to the 8 left. With so few rows the band differs too: R works out its width from a table approximating two of the fit's statistics, where Giggly works out the first of them exactly, so R's band is about a tenth wider. Giggly's curve sits up to 15 points from R's, and its band's edges up to 23.
p
|> Plot.scale_x_continuous(limits: {325, 500})
|> render.()
Limits on the coordinate system
# Setting the limits on the coordinate system performs a visual zoom.
# The data is unchanged, and we just view a small portion of the original
# plot. Note how smooth continues past the points visible on this plot.
p + coord_cartesian(xlim = c(325, 500))
Not in Giggly yet: coord_cartesian()'s xlim option.
Exact limits
# By default, the same expansion factor is applied as when setting scale
# limits. You can set the limits precisely by setting expand = FALSE
p + coord_cartesian(xlim = c(325, 500), expand = FALSE)
Not in Giggly yet: coord_cartesian()'s xlim and expand options.
No expansion
# Similarly, we can use expand = FALSE to turn off expansion with the
# default limits
p + coord_cartesian(expand = FALSE)
Not in Giggly yet: coord_cartesian()'s expand option.
A fixed aspect ratio
# Using a fixed ratio: 1 y-axis unit is 100 x-axis units
# Plot window can be resized and aspect ratio will be maintained
p + coord_cartesian(ratio = 100)
Not in Giggly yet: coord_cartesian()'s ratio option.
A 2d histogram
# You can see the same thing with this 2d histogram
d <- ggplot(diamonds, aes(carat, price)) +
stat_bin_2d(bins = 25, colour = "white")
d
Not in Giggly yet: stat_bin_2d().
Zooming the histogram's scale
# When zooming the scale, the we get 25 new bins that are the same
# size on the plot, but represent smaller regions of the data space
d + scale_x_continuous(limits = c(0, 1))
Not in Giggly yet: stat_bin_2d().
Zooming the histogram's coordinates
# When zooming the coordinate system, we see a subset of original 50 bins,
# displayed bigger
d + coord_cartesian(xlim = c(0, 1))
Not in Giggly yet: stat_bin_2d() and coord_cartesian()'s xlim option.