【问题标题】:Adding specific ticks with date labels from a date variable on x-axis从 x 轴上的日期变量中添加带有日期标签的特定刻度
【发布时间】:2017-03-19 13:45:25
【问题描述】:

我有一个带有两个变量的 df:

Observations: 342
Variables: 2
$ date   <date> 2016-04-02, 2016-04-03, 2016-04-04, 2016-04-05, 2016-04-06, 2016-04-07, 2016-04-08, 2016...
$ visits <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...

我想根据日期绘制访问量,但在我的 x 轴上,我希望在某些特殊事件发生时查看日期的一些特定刻度(以显示之后立即出现更多访问量)。代码现在当然会显示一些日期,但我想添加一些日期,比如“2016-08-10”和“2017-01-25”。

如何扩展我的代码以显示带有标签的附加刻度?

p = ggplot(df, aes(x = date, y = visits)) + 
    geom_line(colour = "purple")

p 

【问题讨论】:

    标签: r date ggplot2 label timeserieschart


    【解决方案1】:

    你可以使用scale_x_date:

    set.seed(666)
    df = data.frame(date = seq(as.Date("2016/4/1"), as.Date("2016/4/30"), by = "day"), visits = round(runif(30, 0, 30)))
    
    ggplot(df, aes(x = date, y = visits)) + 
    geom_line(colour = "purple") + 
    scale_x_date(breaks = c(as.Date("2016-04-10"), as.Date("2016-04-20")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2016-02-18
      • 2019-03-02
      • 2015-06-28
      • 1970-01-01
      相关资源
      最近更新 更多