【问题标题】:R / ggplot2: Can't change scale limits with dates [duplicate]R / ggplot2:无法更改日期的比例限制[重复]
【发布时间】:2020-02-08 21:55:18
【问题描述】:

我正在尝试使用 scale_x_continuous 更改基于日期的 x 轴的比例限制,但 ggplot2 不会接受我的新限制。

示例::

mydata <- tibble::tibble(
    x = as.Date(c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05")),
    y = 1:5
)

mydata_sub <- mydata[2:4,]

g <- ggplot2::ggplot(mydata_sub, ggplot2::aes(x = x, y = y)) +
    ggplot2::geom_point()

g <- g + ggplot2::scale_x_continuous(limits = c(min(mydata$x), max(mydata$x)))

print(g)

这会导致错误信息:

Error in as.Date.numeric(value) : 'origin' must be supplied

我是否使用了错误的scale_ 函数?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    由于您的 x 轴是日期格式,您需要使用 scale_x_date 来操作您的 x 轴:

    library(ggplot2)
    ggplot(mydata_sub, aes(x = x, y = y))+
      geom_point()+
      scale_x_date(limits = c(min(mydata$x), max(mydata$x)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多