【问题标题】:How do I get ggplot to break x-axis on each day?如何让 ggplot 每天打破 x 轴?
【发布时间】:2018-02-03 12:37:56
【问题描述】:

我想要一个显示每天一定数量的条形图。但是,ggplot 并没有单独显示日期,而是每月汇总。如何让 ggplot 每天打破 x 轴?

这是我正在使用的代码:

ggplot(aes(x=d.data$a, y= d.data$b), data = d.data) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_x_date(breaks = '1 day') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

上面的代码运行后出现如下错误:Error in strsplit(unitspec, " ") : non-character argument

以下数据用作输入:

d.data <- structure(list(a = structure(c(16771, 16772, 16773, 16774, 16776, 
    16777, 16780, 16781, 16782, 16784, 16785, 16787, 16788, 16789, 
    16790, 16791, 16792, 16796, 16797, 16798, 16799, 16800, 16801, 
    16802, 16803, 16804, 16805, 16806, 16807, 16808, 16809, 16810, 
    16811, 16812, 16813, 16814, 16815, 16816, 16817, 16818, 16819, 
    16820, 16821, 16822, 16823, 16824, 16825, 16826, 16827, 16828, 
    16829, 16830, 16831, 16832, 16833, 16834, 16835, 16836, 16837
    ), class = "Date"), b = c(5613L, 1374L, 6179L, 2913L, 6628L, 
    3265L, 1763L, 10678L, 7308L, 8686L, 11805L, 4988L, 6584L, 11847L, 
    271L, 8125L, 11227L, 6969L, 8407L, 5083L, 8188L, 10500L, 4592L, 
    8691L, 1121L, 1150L, 3154L, 11724L, 6059L, 2573L, 10244L, 1008L, 
    10938L, 7356L, 1931L, 5182L, 1541L, 10449L, 9948L, 2418L, 10384L, 
    11416L, 8994L, 10652L, 6231L, 3777L, 6079L, 10041L, 10922L, 7410L, 
    1695L, 10890L, 390L, 5635L, 6882L, 6892L, 2807L, 4472L, 5696L
    )), .Names = c("a", "b"), row.names = c(NA, -59L), class = "data.frame")

【问题讨论】:

    标签: r date time-series bar-chart


    【解决方案1】:

    虽然您可以在scale_x_date() 中指定breaks,但为了使用格式指定每日休息时间,正确的参数是date_breaks。您可以通过运行?scale_x_date,在scale_x_date() 的帮助中查看其使用示例。

    因此您的代码应为:

    ggplot(aes(x=a, y= b), data = d.data) +
      geom_col(position = 'dodge') +
      scale_x_date(date_breaks = '1 day') +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
    

    【讨论】:

    • 没错,但问题是为什么代码会抛出错误,以及第一行的编写方式与此完全零关系。
    • 您收到该错误是因为您以错误的格式导入数据。我的答案中没有包含转换,原因很简单,如果原始问题中使用的数据不是Date 格式化的,则会抛出该错误而不是引用的错误。由于出现问题中的错误,a列显然已经是Date
    【解决方案2】:

    使用您的数据,您首先需要确保您的日期列来自 POSIXct 类,然后您可以使用下面的 ggplot 代码:

    d.data$a <- as.POSIXct(d.data$a)
    
    ggplot(aes(x = a, y = b), data = d.data) +
      geom_bar(stat = 'identity', position = 'dodge') +
      scale_x_datetime(date_breaks = "1 day") +     ### changed
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
    

    这会产生以下情节:

    【讨论】:

    • 日期列应该是POSIXct 而不仅仅是Date 是否有任何特殊原因。它会改变ggplot 的呈现方式吗?
    • @Eumenedies 我首先在Date 的课堂上尝试过。这返回了以下错误:Error: Invalid input: time_trans works with objects of class POSIXct only
    • 不就是因为你用了scale_x_datetime而不是scale_x_date吗?
    • @Eumenedies True:scale_x_datetime 期望类 POSIXctscale_x_date 期望类 Datescale_x_time 期望类 hms
    【解决方案3】:

    似乎命令已更改 *see (ggplot2 scale x date?):参数似乎已更改为 date_breaks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2022-12-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      相关资源
      最近更新 更多