在POSIXct时间上绘图时,使用ggplot设置x轴最小值和最大值

时间:2022-09-28 10:10:37

I am trying to plot values over time and am getting the error:

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

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

I worked through earlier problems with this post as well as the post here. I want to set the min and max of the x- axis to remove the blanks space on the extreme ends of plot where there is no data.

我通过这篇文章以及这里的帖子解决了早期的问题。我想设置x轴的最小值和最大值,以删除没有数据的绘图极端的空白空间。

This code calls a plot, but without the corrected x- axis.

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

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

I tried to set the limits of the x-axis using scale_x_date with the code below - which produces the error.

我尝试使用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")))

A dput() of my data are below. I am using the newest version of R and am running Windows 7.

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

Thanks in advance.

提前致谢。

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))

1 个解决方案

#1


3  

Because your x variable is of class as.POSIXct you should use scale_x_datetime instead. scale_x_date is for variables of class Date (as the error message may suggest).

因为你的x变量的类是as.POSIXct,所以你应该使用scale_x_datetime。 scale_x_date用于类Date的变量(如错误消息所示)。

"To remove the blanks space on the extreme ends of plot", you may use the expand argument in your scale call (see here). Setting expand to zero, removes the default, small gap between data and axes.

“要删除绘图末端的空白空间”,您可以在缩放调用中使用expand参数(请参见此处)。设置展开为零,删除数据和轴之间的默认小间隙。

You may also have a look at this post to use the limits argument in the scale call (or xlim), or use coord_cartesian to 'zoom'.

您也可以查看此帖子以在比例调用(或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))

在POSIXct时间上绘图时,使用ggplot设置x轴最小值和最大值

#1


3  

Because your x variable is of class as.POSIXct you should use scale_x_datetime instead. scale_x_date is for variables of class Date (as the error message may suggest).

因为你的x变量的类是as.POSIXct,所以你应该使用scale_x_datetime。 scale_x_date用于类Date的变量(如错误消息所示)。

"To remove the blanks space on the extreme ends of plot", you may use the expand argument in your scale call (see here). Setting expand to zero, removes the default, small gap between data and axes.

“要删除绘图末端的空白空间”,您可以在缩放调用中使用expand参数(请参见此处)。设置展开为零,删除数据和轴之间的默认小间隙。

You may also have a look at this post to use the limits argument in the scale call (or xlim), or use coord_cartesian to 'zoom'.

您也可以查看此帖子以在比例调用(或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))

在POSIXct时间上绘图时,使用ggplot设置x轴最小值和最大值