【问题标题】:Plot data with one column as the major tick and another as the minor绘制数据,一列作为主要刻度,另一列作为次要刻度
【发布时间】:2014-05-07 09:18:18
【问题描述】:

我已经在一周内按小时汇总了一些时间序列数据。为此,我拆分了日期时间并在数据框中生成了两列——一列表示小时,另一列表示星期几。

我想用 x 轴主要刻度标签作为星期几和次要刻度作为一天中的小时以升序绘制整个数据。

这是我正在使用的代码,但它只针对一列绘制它。我是否应该在绘图之前再次将我的两列转换为一个时间戳?

dotplot( Vol ~ Day.Hour, data=merged, type="l",xlab="Time(Day.Hour)",ylab="Number of People Departure/Arrival")

我的数据与此类似:

Day.Hour     Hour      Day      Vol
   Sun.0        0      Sun     2557
   Fri.3        3      Fri     4050

【问题讨论】:

  • dotplot 是一个已失效的 R 函数。

标签: r plot time-series data-visualization axis-labels


【解决方案1】:
#reproducible example
merged = read.table(header=T, sep=" ", text="
Day.Hour Hour Day Vol
Sun.0 0 Sun 2557
Sun.3 3 Sun 1234
Sun.6 6 Sun 2045
Sun.9 9 Sun 4050")

在 df 中添加一个带有序列值的列。我假设您的时间序列有一个固定且连续的时间间隔

merged = data.frame(merged,Seq=seq(1,nrow(merged))) #add column Seq
merged #visualize the new data

  Day.Hour Hour Day  Vol Seq
1    Sun.0    0 Sun 2557   1
2    Sun.3    3 Sun 1234   2
3    Sun.6    6 Sun 2045   3
4    Sun.9    9 Sun 4050   4

用两个“x”轴分别绘制小时和工作日的折线图。

plot(merged$Seq, merged$Vol, 
     type="l",
     xlab="Time(Day.Hour)",ylab="Number of People Departure/Arrival",
     col.axis="white", xaxt='n', yaxt='n')
axis(side=1, at=merged$Seq, labels = merged$Hour,tick=F)
axis(side=1, at=merged$Seq, labels = merged$Day, padj=1.5)
axis(side=2, at=round(seq(min(merged$Vol),
                          max(merged$Vol),
                          by=((max(merged$Vol)-min(merged$Vol))/3)),0)) #plot only four values in y axis starting on minimum and ending on maximum

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2013-07-17
    • 2017-02-27
    • 1970-01-01
    • 2016-09-29
    相关资源
    最近更新 更多