我已经成功将股票市场的日期和时间数据转换为POSIXct并进行了绘图。但是,由于市场在特定时间开放和关闭,我的图表在闭市期间下方出现了长线连接,看起来很尴尬。
我希望我的图表显示如下,在下方 ,期间不可见,并且日期从星期一开始。我希望能在这方面得到帮助。以下是我的代码和一些示例数据。
hongkongstocks <- read.csv(file="Data/hong-kong-stocks-copy.csv", stringsAsFactors = FALSE)
dateOnlyhongkongstocks <- as.POSIXct(hongkongstocks$Date, format="%m/%d/%y %H:%M" #format time)
ggplot(hongkongstocks, aes(x=dateOnlyhongkongstocks, y=Hang.Seng)) + geom_line()
Sample data
Date Hang.Seng
5/25/20 9:30 100.00
5/25/20 9:35 98.28
5/25/20 9:40 98.46
5/25/20 9:45 99.11
这是上述图表中的几天数据。
Date,Hang Seng
5/25/20 9:30,100
5/25/20 9:35,98.28
5/25/20 9:40,98.46
5/25/20 9:45,99.11
5/25/20 9:50,99.74
5/25/20 9:55,100.04
5/25/20 10:00,99.63
5/25/20 10:05,99.77
5/25/20 10:10,99.34
5/25/20 10:20,99.37
5/25/20 10:25,99.06
5/25/20 10:30,99.13
5/25/20 10:40,98.76
5/25/20 10:45,98.72
5/25/20 10:50,98.62
5/25/20 10:55,98.74
5/25/20 11:00,98.64
5/25/20 11:05,98.71
5/25/20 11:10,98.93
5/25/20 11:15,99.23
5/25/20 11:20,98.99
5/25/20 11:30,99.09
5/25/20 11:40,99.02
5/25/20 11:45,99.05
5/25/20 11:50,99.04
5/25/20 12:00,99
5/25/20 13:05,99.24
5/25/20 13:10,99.19
5/25/20 13:15,99.27
5/25/20 13:20,99.32
5/25/20 13:25,99.3
5/25/20 13:30,99.33
5/25/20 13:35,99.49
5/25/20 13:50,99.26
5/25/20 13:55,99.21
5/25/20 14:00,99.35
5/25/20 14:05,99.53
5/25/20 14:10,99.48
5/25/20 14:15,99.51
5/25/20 14:25,99.5
5/25/20 14:30,99.57
5/25/20 14:35,99.61
5/25/20 14:40,99.76
5/25/20 14:45,99.75
5/25/20 14:50,99.83
5/25/20 14:55,99.97
5/25/20 15:00,100.08
5/25/20 15:05,99.96
5/25/20 15:10,99.88
5/25/20 15:15,99.87
5/25/20 15:40,99.94
5/25/20 15:45,99.98
5/25/20 15:50,99.99
5/25/20 15:55,100.06
5/25/20 16:00,100.12
5/25/20 16:05,100.1
5/26/20 9:35,101.41
5/26/20 9:40,101.78
5/26/20 9:45,102.05
5/26/20 9:50,101.83
5/26/20 9:55,101.6
5/26/20 10:00,101.82
5/26/20 10:05,101.77
5/26/20 10:10,101.92
5/26/20 10:15,101.9
5/26/20 10:20,101.98
5/26/20 10:25,101.97
5/26/20 10:40,101.86
5/26/20 10:50,101.61
5/26/20 10:55,101.79
5/26/20 11:00,101.8
5/26/20 11:05,101.93
5/26/20 11:10,101.99
5/26/20 11:15,101.84
5/26/20 11:20,101.74
5/26/20 11:35,101.85
5/26/20 11:40,101.88
5/26/20 11:55,101.94
5/26/20 13:05,102.18
5/26/20 13:10,102.09
5/26/20 13:15,102.01
5/26/20 13:20,102.02
5/26/20 13:30,101.95
5/26/20 13:35,101.96
5/26/20 13:40,102.06
5/26/20 13:45,102.12
5/26/20 13:50,102.1
5/26/20 13:55,102.22
5/26/20 14:00,102.17
5/26/20 14:05,102.26
5/26/20 14:10,102.23
5/26/20 14:20,102.24
5/26/20 14:25,102.27
5/26/20 14:30,102.3
5/26/20 14:35,102.39
5/26/20 14:40,102.36
5/26/20 14:45,102.34
5/26/20 14:50,102.25
5/26/20 15:00,102.21
5/26/20 15:20,102.13
5/26/20 15:45,102.04
5/26/20 15:55,102.14
scale_x_datetime
x 轴来完成此操作,因为 x 值不是连续的。我认为你最好的选择是将时间重新缩放为数字值,然后使用自定义标记的刻度进行绘图。 - Allan Cameronscale_x_datetime
这样的日期刻度是连续的,对吧?OP,我认为你需要从数据中删除数据下降的时间段。由于这是在收盘时发生的,所以应该是一段固定的时间,因此似乎可以通过一些代码剪切或过滤掉那段时间内的数据。你能否在这里与我们分享几天的数据?如果不太大,最好直接在你的问题中共享dput(hongkongstocks)
的输出,作为代码粘贴/格式化。 - chemdork123