【问题标题】:Data on secondary axis in GGPlot [duplicate]GGPlot 中的辅助轴上的数据 [重复]
【发布时间】:2021-12-23 08:25:47
【问题描述】:

我一直在努力在 R 中添加辅助轴,真的需要帮助。我的数据框包括一段时间内 A 井的水位变化和 B 井抽水的水位。

我的数据框如下:

   Time_Date             Level_Change        Level
   2021-11-03 07:45:00   0.5                 18.5
   2021-11-03 08:00:00   0.6                 18.9
   2021-11-03 08:15:00   0.4                 19.5
   2021-11-03 08:30:00   0.7                 20.5
   2021-11-03 08:45:00   0.9                 21.4

我要做的是在主要(左)y 轴上绘制 Level_Change,在次要(右)y 轴上绘制 Level。如您所见,它们有两个非常不同的范围。我之前尝试过,但是当我添加 sec.axis 时,我的第一个轴总是搞砸了。为了澄清,我将添加辅助轴,然后我的主轴也将具有 0-30 的范围,这使得查看数据变得困难。

这是我在下面使用的代码:

   Data %>%
   ggplot(aes(x = Time_Date, y = Level_Change, color = id)) +
   geom_point(size = 1) +
   geom_line(aes(x=Time_Date, y = MW25)) +
   scale_color_manual(values=c("red", "blue", "yellow", "green", "purple")) + 
   scale_x_datetime(breaks = scales::date_breaks("1 hour"), date_labels = "%H:%M") +
   scale_y_continuous(sec.axis = sec_axis(~.+15.12, name = "MW-25 GW Level")) +
   labs(title = "MW-25 Pumping Test Monitoring Well Response",
   x = "",
   y = "Change in Water Level (ft.)",
   legend = "Well ID") +
   theme(legend.title = element_blank()) + 
   theme_classic()

真的需要我能得到的任何帮助!

谢谢

【问题讨论】:

  • 你尝试了什么?你到底是在哪里卡住的?你试过stackoverflow.com/questions/3099219/…的方法吗?在没有任何区分细节的情况下,这似乎只是该问题的重复。
  • 我已经添加了我使用的代码。

标签: r ggplot2 axes


【解决方案1】:

感谢提供的链接,我能够回答这个问题。这里的代码有效:

ggplot with 2 y axes on each side and different scales

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 2021-03-14
    • 1970-01-01
    • 2019-09-19
    • 2020-11-29
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多