You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R ggplot2如何重新排序x轴使0值位于x轴最右端

R ggplot2反转x轴实现yrBP 0值居右的解决方案

你只需要在ggplot图层中添加scale_x_reverse()即可实现连续x轴的反转,让yrBP数值从左到右由大到小排列,0值自然展示在x轴最右侧。

修改后的完整代码如下:

combined_co2%>%
  rename(carbon_dioxide_ppmv=co2)%>%
  ggplot(mapping =aes(x = yrBP, y = carbon_dioxide_ppmv))+
  geom_line() +
  # 新增该行反转x轴
  scale_x_reverse() -> g_line

g_line+
  annotate( geom ="curve",
            x  = 2e+05,
            y  = 400,
            xend = 280,
            yend  = 400,
            curvature = 0.3,
            arrow = arrow(length=unit(1.5, "mm")),
            alpha = 0.8, 
            colour = "#00BFC4") ->g_curve

g_curve+
  annotate(
    geom = "text",
    x = 3e+05,
    y = 400,
    label = "2021 average:400 ppmv",
    hjust = 0.1, vjust = -0.1,
    lineheight = 0.8, 
    colour= "#00BFC4"
  )

补充说明:

  • 若需要自定义x轴刻度、显示范围,可直接在scale_x_reverse()中传入参数调整,示例:
    scale_x_reverse(limits = c(400000, 0), breaks = seq(0, 400000, 100000))
    以上参数可固定x轴显示范围为距今40万年到0年,每10万年显示一个刻度。
  • 原有代码中的标注坐标无需调整,scale_x_reverse()仅修改轴的显示顺序,不会改变数值映射逻辑。

内容的提问来源于stack exchange,提问作者joeyops

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 05:06:03