ggplot2绘图结果往往X轴和Y轴的截距交点往往不是原点:譬如下图y轴就不是起始于0

R语言-ggplot原点设置 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

调整坐标起始位点可以利用scale_y_continuous(expand = c(0, 0))或者scale_x_continuous(expand = c(0, 0))

expand的解释如下

expand

A numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes. The defaults are c(0.05, 0) for continuous variables, and c(0, 0.6)for discrete variables.

 

连续性和离散型都涉及,默认值不同,离散型scale_y_discrete 或者scale_x_discrete,这里针对连续性进行介绍:

 

参考画图数据和代码   http://www.omicsclass.com/article/92

修改y轴连续性坐标起点:y = p + scale_y_continuous(expand=c(0,0))

R语言-ggplot原点设置 随笔 第2张

如果针对x离散型的调整呢?尝试调整数,使柱子紧贴Y轴:x = p + scale_x_discrete(expand=c(0,0))

R语言-ggplot原点设置 随笔 第3张
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄