Graphics Reference
In-Depth Information
Figure 8-27. Left: scatter plot with log 10 x- and y-axes, and with manually specified breaks; right:
with exponents for the tick labels
To instead use exponential notation for the break labels ( Figure 8-27 , right), use the
trans_format() function, from the scales package:
library(scales)
p + scale_x_log10(breaks = 10 ^ ( -1 : 5 ),
labels = trans_format( "log10" , math_format( 10 ^ . x))) +
scale_y_log10(breaks = 10 ^ ( 0 : 3 ),
labels = trans_format( "log10" , math_format( 10 ^ . x)))
Another way to use log axes is to transform the data before mapping it to the xand ycoordinates
( Figure 8-28 ) . Technically, the axes are still linear—it's the quantity that is log-transformed:
ggplot(Animals, aes(x = log10(body), y = log10(brain), label = rownames(Animals))) +
geom_text(size = 3 )
The previous examples used a log 10 transformation, but it is possible to use other transforma-
tions, such as log 2 and natural log, as shown in Figure 8-29 . It's a bit more complicated to use
these— scale_x_log10() is shorthand, but for these other log scales, we need to spell them
out:
library(scales)
# Use natural log on x, and log2 on y
p + scale_x_continuous(trans = log_trans(),
Search WWH ::




Custom Search