ggplot使用scale_x_continuous后添加轴标签触发重复刻度报错求助
警告触发原因
- 你调用了两个作用于x轴尺度的函数产生了冲突:
scale_x_continuous()和xlim()都是用来定义x轴连续尺度的函数,xlim()本质是scale_x_continuous(limits = ...)的快捷写法,重复定义会导致后添加的尺度覆盖前者,你之前设置的n.breaks = 10参数也会随之失效。 - 该警告和
labs()内的坐标轴标签设置无关,你当前的标签写法是正确的,无需调整。
修正方案
将x轴、y轴的范围参数直接整合到对应的scale_*_continuous()函数内即可消除警告,也可以选择用coord_cartesian()设置显示范围(不会剔除超出范围的原始数据,仅调整可视区间)。
修正后代码
MigrPlt = ggplot(Migr,aes(Distance3, `newBa`))+ geom_line()+ # 合并x轴刻度数、范围参数到同一个尺度定义中 scale_x_continuous(n.breaks = 10, limits = c(0, 3800))+ # 合并y轴范围参数到y轴尺度定义中 scale_y_continuous(limits = c(0,25)) + labs(y= expression (paste('Ba:Ca (µmol mol'^~-1,')')), x = "Distance from Core (μm)") + geom_hline(yintercept=2.25, linetype="dashed", color = "red")+ geom_vline(xintercept = 1193.9, linetype="dashed", color = "grey")+ geom_vline(xintercept = 1965.5, linetype="dashed", color = "grey") + geom_vline(xintercept = 2616.9, linetype="dashed", color = "grey") + geom_vline(xintercept = 3202.8, linetype="dashed", color = "grey")+ geom_vline(xintercept = 3698.9, linetype="dashed", color = "grey")
如果需要保留超出范围的原始数据、仅调整显示区间,可将上述scale_*中的limits参数删除,替换为:
+ coord_cartesian(xlim = c(0,3800), ylim = c(0,25))
内容的提问来源于stack exchange,提问作者Angela Russell
相关产品推荐
相关产品推荐

