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().
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 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 in Giggly yet: the msleep dataset, scales::trans_breaks(), scales::trans_format() and
scales::math_format(), and annotation_logticks() and its sides option.
Ticks outside the panel
a + annotation_logticks(sides = "lr", outside = TRUE) +
coord_cartesian(clip = "off") # Ticks outside plot
Not in Giggly yet: everything a needs (the msleep dataset, scales::trans_breaks(),
scales::trans_format() and scales::math_format()), annotation_logticks() and its outside
option, and coord_cartesian() and its clip option.
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 in Giggly yet: everything a needs (the msleep dataset, scales::trans_breaks(),
scales::trans_format() and scales::math_format()), and annotation_logticks().
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 in Giggly yet: the msleep dataset, scales::label_math() and annotation_logticks().
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 in Giggly yet: the msleep dataset, coord_transform(), and annotation_logticks() and its
scaled option.
Tick lengths
# Change the length of the ticks
a + annotation_logticks(
short = unit(.5,"mm"),
mid = unit(3,"mm"),
long = unit(4,"mm")
)
Not in Giggly yet: everything a needs (the msleep dataset, scales::trans_breaks(),
scales::trans_format() and scales::math_format()), and annotation_logticks() and its short,
mid and long options, lengths given as grid units.