当我们用ggplot2画图时(以mtcars为例):
data(mtcars)
p <- ggplot(data = mtcars, aes(x = wt, y = drat)) + geom_point(color = 'steelblue') + coord_cartesian(xlim = c(0,5), ylim = c(0,6)) + theme_bw()
p
可以发现x轴和y轴的0点都不在原点的位置,会空出一小块,这样如果我们在加y=x等线条的时候回发生如下问题:
p + geom_abline(slope = 1, intercept = 0)
感觉y=x这条线会稍微高一点。此外,如果不给定xlim和ylim的话会是这样:
ggplot(data = mtcars, aes(x = wt, y = drat)) + geom_point(color = 'steelblue') + geom_abline(slope = 1, intercept = 0)
如果我们想要xy轴刻度从左下角开始可以这样:
p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + geom_abline(slope = 1, intercept = 0)