【发布时间】:2019-11-23 13:30:36
【问题描述】:
如何将轴范围设置为与指定的完全一致?
对于以下示例,我希望 x 轴在 0% 处交叉(而不是如图所示的 -0.5%),并且我希望最大值为 12%(而不是 12.5%)
我都试过了:
scale_x_continuous(limits = c(0, 0.12))
和
coord_cartesian(ylim = c(0, 0.12))
根据:
How to set limits for axes in ggplot2 R plots?
Can I limit ggplot axis range EXACTLY?
示例代码:
myData = data.frame(x = c(0, 1, 2, 3, 4, 5),
y = c(0.05,0.06, 0.07, 0.08, 0.09, 0.09))
ggplot() +
geom_step(data=myData, aes(x=x, y=y), color='orange', size=1) +
xlab('') +
ylab('') +
scale_y_continuous(labels = scales::percent, limits=c(0,0.12))
ggplot() +
geom_step(data=myData, aes(x=x, y=y), color='blue', size=1) +
xlab('') +
ylab('') +
scale_y_continuous(labels = scales::percent) +
coord_cartesian(ylim = c(0,0.12))
结果:
【问题讨论】: