写在前面。
x
轴和y
轴为解读图片所呈现的数据提供了上下文信息。
ggplot2以默认设置显示的坐标轴在多数情况下看起来都不错,不过也可以通过一些选项进行细节的控制。
下文将介绍微调坐标轴标签
、刻度线数量
和布局
以及刻度线标签
等元素。
对数坐标轴
有些时候,数据的范围跨越了几个数量级
,这个时候为了更合理的呈现数据,可能会需要用到对数坐标轴
。
为对数坐标轴添加刻度
如何为对数坐标轴添加间距递减的刻度线?
示例数据Animals
数据集。
> library(MASS)
> library(scales)
> str(Animals)
'data.frame': 28 obs. of 2 variables:
$ body : num 1.35 465 36.33 27.66 1.04 ...
$ brain: num 8.1 423 119.5 115 5.5 ...
使用annotation_logticks()
:
ggplot(Animals, aes(x = body, y = brain, label = rownames(Animals))) +
geom_text(size = 3) +
annotation_logticks() +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
[图片上传失败...(image-c2a61f-1699158098423)]
以上。