Log tick marks: annotation_logticks
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
About this notebook
This notebook follows the examples on ggplot2's reference page for
annotation_logticks(), 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.
annotation_logticks() draws tick marks for a log scale inside the panel, closer together towards
each power of the base: long ones at the powers, middle ones at the fives and short ones between, so
they suit base 10 best. It is superseded by guide_axis_logticks(). It expects data already on a
log scale, through scale_x_log10() and scale_y_log10() or by taking logs before plotting; under
coord_transform(), which takes the logs itself, it needs scaled = FALSE. Giggly has log scales,
Giggly.Plot.scale_x_log10/2 and Giggly.Plot.scale_y_log10/2, but no annotation_logticks(),
since ggplot2 has superseded it, and its replacement draws a different plot: guide_axis_logticks()
draws the ticks on the axis, outside the panel, where annotation_logticks() draws them inside. So
no example here is drawn, and none names what it waits for.
Log ticks on chosen sides
# Make a log-log plot (without log ticks)
a <- ggplot(msleep, aes(bodywt, brainwt)) +
geom_point(na.rm = TRUE) +
scale_x_log10(
breaks = scales::trans_breaks("log10", \(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
) +
scale_y_log10(
breaks = scales::trans_breaks("log10", \(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
) +
theme_bw()
a + annotation_logticks() # Default: log ticks on bottom and left
a + annotation_logticks(sides = "lr") # Log ticks for y, on left and right
a + annotation_logticks(sides = "trbl") # All four sides
ggplot2's msleep, which Giggly has as Giggly.Datasets.msleep/0, describes the sleep of
mammals. Some lack a brain weight, and na.rm = TRUE drops them without a warning, as Giggly drops
any row missing a position.
Not drawn: Giggly has no annotation_logticks(), as the About says.
Ticks outside the panel
a + annotation_logticks(sides = "lr", outside = TRUE) +
coord_cartesian(clip = "off") # Ticks outside plot
Not drawn: Giggly has no annotation_logticks(), as the About says.
Without the minor grid lines
# Hide the minor grid lines because they don't align with the ticks
a + annotation_logticks(sides = "trbl") + theme(panel.grid.minor = element_blank())
Not drawn: Giggly has no annotation_logticks(), as the About says.
Taking logs first
# Another way to get the same results as 'a' above: log-transform the data before
# plotting it. Also hide the minor grid lines.
b <- ggplot(msleep, aes(log10(bodywt), log10(brainwt))) +
geom_point(na.rm = TRUE) +
scale_x_continuous(name = "body", labels = scales::label_math(10^.x)) +
scale_y_continuous(name = "brain", labels = scales::label_math(10^.x)) +
theme_bw() + theme(panel.grid.minor = element_blank())
b + annotation_logticks()
Not drawn: Giggly has no annotation_logticks(), as the About says.
Log coordinates
# Using a coordinate transform requires scaled = FALSE
t <- ggplot(msleep, aes(bodywt, brainwt)) +
geom_point() +
coord_transform(x = "log10", y = "log10") +
theme_bw()
t + annotation_logticks(scaled = FALSE)
Not drawn: Giggly has no annotation_logticks(), as the About says.
Tick lengths
# Change the length of the ticks
a + annotation_logticks(
short = unit(.5,"mm"),
mid = unit(3,"mm"),
long = unit(4,"mm")
)
Not drawn: Giggly has no annotation_logticks(), as the About says.