【问题标题】:Setting the x-axis min and max in using ggplot when ploting over POSIXct time在 POSIXct 时间上绘制时使用 ggplot 设置 x 轴最小值和最大值
【发布时间】:2015-02-05 22:02:20
【问题描述】:

我试图随着时间的推移绘制值并收到错误:

Error: Invalid input: date_trans works with objects of class Date only

我解决了this postthe post here 的早期问题。我想设置 x 轴的最小值和最大值,以删除没有数据的绘图末端的空白空间。

此代码调用绘图,但没有更正的 x 轴。

ggplot(dta, aes(x= WeekStarting, y = AvgWeekEle, group = IndID))+
        geom_line(size = 1)+ 
        theme(axis.text.x=element_text(angle=30, hjust=1))

我尝试使用scale_x_date 和下面的代码设置 x 轴的限制 - 这会产生错误。

ggplot(dta, aes(x= WeekStarting, y = AvgWeekEle, group = IndID))+
        geom_line(size = 1)+ 
        theme(axis.text.x=element_text(angle=30, hjust=1))+
        scale_x_date(breaks = "1 week", labels=date_format("%b-%d-%Y"), 
            limits = as.Date(c("2014-01-6","2014-02-15")))

我的数据中的dput() 如下。我正在使用最新版本的 R 并且正在运行 Windows 7。

提前致谢。

dta <- structure(list(IndID = c("BHS_011_A", "BHS_011_A", "BHS_011_A", 
"BHS_011_A", "BHS_011_A", "BHS_011_A", "BHS_011_A", "BHS_011_A", 
"BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_015_A", 
"BHS_015_A", "BHS_015_A", "BHS_015_A", "BHS_018_A", "BHS_018_A", 
"BHS_018_A", "BHS_018_A", "BHS_018_A", "BHS_018_A", "BHS_018_A", 
"BHS_018_A"), WeekStarting = structure(c(1388905200, 1389510000, 
1390114800, 1390719600, 1391324400, 1391929200, 1392534000, 1393138800, 
1388905200, 1389510000, 1390114800, 1390719600, 1391324400, 1391929200, 
1392534000, 1393138800, 1388905200, 1389510000, 1390114800, 1390719600, 
1391324400, 1391929200, 1392534000, 1393138800), class = c("POSIXct", 
"POSIXt"), tzone = ""), AvgWeekEle = c(1717.21428571429, 1770.07142857143, 
1737.53571428571, 1673.32142857143, 1648.42857142857, 1760.71428571429, 
1668.48148148148, 1654.5, 1643.5, 1794.85714285714, 1740.64285714286, 
1664.73076923077, 1704.73076923077, 1685.2962962963, 1707.89285714286, 
1661.46428571429, 1702.87096774194, 1691.17647058824, 1653.41176470588, 
1650.30303030303, 1741.54545454545, 1652.33333333333, 1573.125, 
1570.82352941176)), .Names = c("IndID", "WeekStarting", "AvgWeekEle"
), class = "data.frame", row.names = c(3362L, 3363L, 3364L, 3365L, 
3366L, 3367L, 3368L, 3369L, 3470L, 3471L, 3472L, 3473L, 3474L, 
3475L, 3476L, 3477L, 3535L, 3536L, 3537L, 3538L, 3539L, 3540L, 
3541L, 3542L))

【问题讨论】:

  • 是的,我在链接的帖子中看到了这个建议。但是按照您的建议,我收到错误“输入无效:time_trans 仅适用于 POSIXct 类的对象”由于两个建议都导致错误,我想我会从头开始。根据您的建议,对第二个错误有什么想法吗?
  • 更改为 scale_x_datetime 对我有用。请注意,您还需要将 limits=as.Date 更改为 limits = as.POSIXct
  • 尴尬和谦卑...谢谢!很乐意接受答案...

标签: r datetime ggplot2 plot posixct


【解决方案1】:

因为您的 x 变量属于 as.POSIXct 类,您应该改用 scale_x_datetimescale_x_date 用于 Date 类的变量(如错误消息所示)。

“要删除绘图末端的空格”,您可以在scale 调用中使用expand 参数(请参阅here)。将expand 设置为零,删除默认的数据和轴之间的小间隙。

您还可以查看this post 以在scale 调用(或xlim)中使用limits 参数,或使用coord_cartesian 来“缩放”。

ggplot(dta, aes(x= WeekStarting, y = AvgWeekEle, group = IndID)) +
  geom_line(size = 1) + 
  theme(axis.text.x=element_text(angle = 30, hjust = 1)) +
  scale_x_datetime(breaks = "1 week", labels = date_format("%b-%d-%Y"),
                   expand = c(0, 0))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2012-06-29
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多