【问题标题】:R, ggplot2, skip printing x valuesR,ggplot2,跳过打印 x 值
【发布时间】:2013-04-01 13:49:46
【问题描述】:

这可能相当简单,但我似乎不知道该怎么做。 我得到了一个不错的情节,其中包含一组值线。 y 代表金额,x 代表日期。

问题很简单,日期太多了,它们被打印在彼此的顶部。

代码:

sp = rbind(sp1,sp2,sp3,sp4)
pm = ggplot(data = sp, aes(x = date, 
                                 y = amount, 
                                 colour=sm, 
                                 group=sm)) + 
        geom_line()

如何使 x 轴仅打印例如每 5 个日期而不是所有日期? 提前致谢!

【问题讨论】:

  • 您是否考虑过使用theme 使它们成为对角线?无论如何,您可能正在scale_x_continuousscale_x_datetime 中寻找breaks 参数。
  • 我同意@sebastian-c 的观点,如果你在datetime 上进行缩放,ggplot 会为你找到不错的休息时间。
  • This, this 甚至 this 帖子应该会给你一个想法..
  • 谢谢我检查一下
  • 暗示使用日期时间刻度的建议是将 sp$date 转换为 Date 对象。

标签: r plot ggplot2


【解决方案1】:
library(scales)

sp = rbind(sp1,sp2,sp3,sp4)
pm = ggplot(data = sp, aes(x = date, y = amount, colour=sm, group=sm)) + 
    geom_line() +
    scale_x_date("x axis title", breaks = "5 years")

scale_x_date 会为你整理出 x 轴标签。要指定标签间隔,请使用上面的 scales 包。 (p.s 你的日期需要是 Date、POSIXct 或 POSIXlt 类)

【讨论】:

  • 感谢您的回复,它有效。但我已经找到了 :) 还是谢谢
猜你喜欢
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多