【问题标题】:plotting on same plot with same x axis在具有相同 x 轴的同一图上绘图
【发布时间】:2012-06-19 07:20:09
【问题描述】:

我有一个这样的时间序列,其中包含过去 4 个月(2 月、3 月、4 月、5 月)的数据:

         "timestamp" "rain_intensity"
  "1" "2012-06-15 01:05:00 UTC" 2.6
  "2" "2012-06-15 01:00:00 UTC" 9.6
  "3" "2012-06-15 00:55:00 UTC" 18.5
  "4" "2012-06-15 00:50:00 UTC" 25.7
  "5" "2012-06-15 00:45:00 UTC" 32.8
  "6" "2012-06-15 00:40:00 UTC" 38.7

我还有一个类似的时间序列,但它包含过去 2 个月(4 月、5 月)的数据。 我必须将它们绘制在 x 轴上方(4 个月数据)和 x 轴下方(2 个月数据)的同一图上。第二个情节。

par 中使用 mfrow 不成功,因为 x 轴不相同。

我该怎么做?

【问题讨论】:

  • 您可以先绘制 4 个月的数据,然后使用 add = TRUE 绘制 2 个月的数据。你也可以使用 ggplot2 并使用 faceting。

标签: r plot time-series


【解决方案1】:

plot 函数中定义参数xlim 可能会有所帮助。

【讨论】:

    【解决方案2】:

    ggplot2 提供了一种非常优雅的方式来表达这一点。这是 Roman 答案的代码 sn-p。

    首先,将数据转换成一种方便的格式,全部放在同一个 data.frame 中。我会假设它看起来像这样

    timestamp variable        value
    [..]      rain_intensity1 2.6     # from the table you show above
    [..]      rain_intensity2 5.4     # from the other table you mention
    

    melt 来自包reshape 有助于进行这种转换。现在剧情

    qplot(timestamp, value, data=my_table, facets= .~variable)
    

    qplot 构面公式是row_var ~ column_var. 在其中一个或另一个为空时代表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      相关资源
      最近更新 更多