在x轴上绘制一周中的时间

时间:2017-10-22 14:10:44

标签: r date plot posixct

我有一个包含小时数和频率的数据集。密谋后:

plot(roundedHours$Var1, roundedHours$Freq, xlab = "time", ylab = "frequency")
lines(roundedHours$Var1, roundedHours$Freq)

生成的图表如下所示:

enter image description here

我想在现有的x轴下方添加第二个x轴,显示星期几。为了看看我是否可以在范围内添加第一天和最后一天,我试过了

axis(side=4, at=c("2017-09-08 20:00:00 EDT","2017-09-15 20:00:00 EDT"), labels=c("Friday","Friday"))

但这会产生错误

Error in axis(side = 4, at = c("2017-09-08 20:00:00 EDT", "2017-09-15 20:00:00 EDT"),  : 
  no locations are finite
In addition: Warning message:
In axis(side = 4, at = c("2017-09-08 20:00:00 EDT", "2017-09-15 20:00:00 EDT"),  :
  NAs introduced by coercion

如何在当前x轴下添加星期几? 这是数据:

> dput(roundedHours)
structure(list(Var1 = structure(c(1504915200, 1504918800, 1504922400, 
1504926000, 1504929600, 1504933200, 1504936800, 1504940400, 1504944000, 
1504947600, 1504951200, 1504954800, 1504958400, 1504962000, 1504965600, 
1504969200, 1504972800, 1504976400, 1504980000, 1504983600, 1504987200, 
1504990800, 1504994400, 1504998000, 1505001600, 1505005200, 1505008800, 
1505012400, 1505016000, 1505019600, 1505023200, 1505026800, 1505030400, 
1505034000, 1505037600, 1505041200, 1505044800, 1505048400, 1505052000, 
1505055600, 1505059200, 1505062800, 1505066400, 1505070000, 1505073600, 
1505077200, 1505080800, 1505084400, 1505088000, 1505091600, 1505095200, 
1505098800, 1505102400, 1505106000, 1505109600, 1505113200, 1505116800, 
1505120400, 1505124000, 1505127600, 1505131200, 1505134800, 1505138400, 
1505142000, 1505145600, 1505149200, 1505152800, 1505156400, 1505160000, 
1505163600, 1505167200, 1505170800, 1505174400, 1505178000, 1505181600, 
1505185200, 1505188800, 1505192400, 1505196000, 1505199600, 1505203200, 
1505206800, 1505210400, 1505214000, 1505217600, 1505221200, 1505224800, 
1505228400, 1505232000, 1505235600, 1505239200, 1505242800, 1505246400, 
1505250000, 1505253600, 1505257200, 1505260800, 1505264400, 1505268000, 
1505271600, 1505275200, 1505278800, 1505282400, 1505286000, 1505289600, 
1505293200, 1505296800, 1505300400, 1505304000, 1505307600, 1505311200, 
1505314800, 1505318400, 1505322000, 1505325600, 1505329200, 1505332800, 
1505336400, 1505340000, 1505343600, 1505347200, 1505350800, 1505354400, 
1505358000, 1505361600, 1505365200, 1505368800, 1505372400, 1505376000, 
1505379600, 1505383200, 1505386800, 1505390400, 1505394000, 1505397600, 
1505401200, 1505404800, 1505408400, 1505412000, 1505415600, 1505419200, 
1505422800, 1505426400, 1505430000, 1505433600, 1505437200, 1505440800, 
1505444400, 1505448000, 1505451600, 1505455200, 1505458800, 1505462400, 
1505466000, 1505469600, 1505473200, 1505476800, 1505480400, 1505484000, 
1505487600, 1505491200, 1505494800, 1505498400, 1505502000, 1505505600, 
1505509200, 1505512800, 1505516400, 1505520000), class = c("POSIXct", 
"POSIXt"), tzone = ""), Freq = c(3332L, 7781L, 7991L, 6456L, 
4511L, 3289L, 3455L, 3397L, 3778L, 2682L, 2512L, 2407L, 3376L, 
3675L, 4992L, 5815L, 6229L, 7696L, 7917L, 8255L, 8356L, 8764L, 
7915L, 8048L, 7016L, 5896L, 7131L, 5808L, 6378L, 6552L, 5090L, 
3798L, 3514L, 3775L, 3390L, 2538L, 3197L, 3552L, 4171L, 5315L, 
6319L, 7740L, 9281L, 9443L, 8704L, 8223L, 7648L, 9663L, 9143L, 
7441L, 6701L, 5058L, 4962L, 4414L, 3767L, 3202L, 3382L, 3528L, 
4176L, 4625L, 6533L, 5890L, 6029L, 6366L, 6722L, 8339L, 7511L, 
8143L, 9661L, 10582L, 11232L, 8612L, 7886L, 8054L, 6646L, 5900L, 
5517L, 3004L, 1895L, 1378L, 2272L, 2599L, 2132L, 3404L, 5035L, 
4749L, 4273L, 4487L, 5698L, 4210L, 7204L, 8176L, 10905L, 9340L, 
10437L, 8771L, 6268L, 6629L, 5508L, 4600L, 4341L, 2243L, 2000L, 
1731L, 1373L, 1800L, 3600L, 3740L, 4168L, 3920L, 5269L, 5879L, 
5483L, 6837L, 7521L, 9513L, 10540L, 10513L, 9815L, 9510L, 8171L, 
6255L, 6731L, 6546L, 4865L, 4835L, 2988L, 3081L, 2646L, 2222L, 
2709L, 4406L, 5205L, 7541L, 6582L, 5871L, 5660L, 8002L, 8421L, 
8480L, 11122L, 11756L, 12756L, 9976L, 9332L, 8458L, 7741L, 8384L, 
5960L, 4638L, 3235L, 2864L, 2597L, 2484L, 2820L, 3182L, 4284L, 
5662L, 5763L, 6217L, 4804L, 7484L, 7981L, 9112L, 9655L, 9578L, 
9669L, 9020L, 3908L), weekday = c("Friday", "Friday", "Friday", 
"Friday", "Saturday", "Saturday", "Saturday", "Saturday", "Saturday", 
"Saturday", "Saturday", "Saturday", "Saturday", "Saturday", "Saturday", 
"Saturday", "Saturday", "Saturday", "Saturday", "Saturday", "Saturday", 
"Saturday", "Saturday", "Saturday", "Saturday", "Saturday", "Saturday", 
"Saturday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", 
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", 
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", 
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Wednesday", "Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Thursday", "Thursday", "Thursday", "Thursday", "Thursday", "Thursday", 
"Thursday", "Thursday", "Thursday", "Thursday", "Thursday", "Thursday", 
"Thursday", "Thursday", "Thursday", "Thursday", "Thursday", "Thursday", 
"Thursday", "Thursday", "Thursday", "Thursday", "Thursday", "Thursday", 
"Friday", "Friday", "Friday", "Friday", "Friday", "Friday", "Friday", 
"Friday", "Friday", "Friday", "Friday", "Friday", "Friday", "Friday", 
"Friday", "Friday", "Friday", "Friday", "Friday", "Friday", "Friday"
)), .Names = c("Var1", "Freq", "weekday"), row.names = c(NA, 
-169L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

这个怎么样?

ax.dt <- seq(as.POSIXct("2017-09-08 0:00:00 EDT"),
             as.POSIXct("2017-10-15 0:00:00 EDT"), by=86400)
axis(side=1, at=ax.dt, labels=weekdays(ax.dt), line=2)

enter image description here