I'm trying to plot 5 days of historical stock data in R using ggplot. Datetime on the x axis, and the stock values ('close') on the y axis. I only want to show the minutes of the day when the stock market is open, and my data set is limited to 7 hours a day of values x 5 days.
我正在尝试使用ggplot在R中绘制5天的历史股票数据。 x轴上的日期时间和y轴上的库存值('close')。我只想显示股票市场开盘当天的会议纪要,我的数据集限制为每天7小时的价值x 5天。
But when I plot it with ggplot, the scale is changed so I get all the hours per day.
但是当我用ggplot绘制它时,比例会改变,所以我每天都得到所有的时间。
ggplot(data = df_stock, aes(x = datetime, y = close)) +
geom_line()
I've tried googling this and using the R help function. I'm quite new to R so my apologies if this is very easy to solve. I hope someone can guide me in the right direction.
我试过谷歌搜索并使用R帮助功能。我对R很陌生,所以我很抱歉,如果这很容易解决的话。我希望有人可以指导我朝着正确的方向前进。
1 个解决方案
#1
1
While a minimal example would be helpful to address this question, a more general answer which could be useful can be given.
虽然最小的例子有助于解决这个问题,但可以给出一个可能有用的更一般的答案。
ggplot2
does not have a facility for axis-breaking, as it's not considered good practice. However what you're doing here is actually a transformation of the variable --- hours open rather than hours of the day. So you'll have to transform the variable itself. You can do this using the lubridate
package. Let's take two days and imagine the market is open between 10am and 5pm.
ggplot2没有断轴设施,因为它不被认为是好习惯。然而,你在这里所做的实际上是变量的变换 - 小时开放而不是一天中的几小时。所以你必须自己转换变量。你可以使用lubridate包来做到这一点。让我们花两天时间想象市场在上午10点到下午5点之间开放。
require(lubridate)
dates <- c("2014-01-01 10:00:00 UTC", "2014-01-01 13:00:00 UTC", "2014-01-01 17:00:00 UTC", "2014-01-02 13:00:00 UTC", "2014-01-02 17:00:00 UTC")
dates <- ymd_hms(dates)
Now you will need to scale the data to only have the times you want. You can call the parts of the date with `lubridate', and divide by the hours in the day, where here they are 24 rather than 7.
现在,您需要将数据缩放到只有您想要的时间。您可以使用`lubridate'调用日期的各个部分,然后除以当天的小时数,这里它们是24而不是7。
hour(dates) <- hour(dates)-10
scaleddates <- day(dates)-1 + hour(dates)/7 + minute(dates)/60/7 + second(dates)/60/60/7
scaleddates
[1] 0.0000000 0.4285714 1.0000000 1.4285714 2.0000000
Now you can plot the graph, with the x-axis reading 'Days' rather than 'Dates'. The x-axis is now how far you are through the working day.
现在您可以绘制图形,x轴读数为“Days”而不是“Dates”。 x轴现在是你工作日的距离。
#1
1
While a minimal example would be helpful to address this question, a more general answer which could be useful can be given.
虽然最小的例子有助于解决这个问题,但可以给出一个可能有用的更一般的答案。
ggplot2
does not have a facility for axis-breaking, as it's not considered good practice. However what you're doing here is actually a transformation of the variable --- hours open rather than hours of the day. So you'll have to transform the variable itself. You can do this using the lubridate
package. Let's take two days and imagine the market is open between 10am and 5pm.
ggplot2没有断轴设施,因为它不被认为是好习惯。然而,你在这里所做的实际上是变量的变换 - 小时开放而不是一天中的几小时。所以你必须自己转换变量。你可以使用lubridate包来做到这一点。让我们花两天时间想象市场在上午10点到下午5点之间开放。
require(lubridate)
dates <- c("2014-01-01 10:00:00 UTC", "2014-01-01 13:00:00 UTC", "2014-01-01 17:00:00 UTC", "2014-01-02 13:00:00 UTC", "2014-01-02 17:00:00 UTC")
dates <- ymd_hms(dates)
Now you will need to scale the data to only have the times you want. You can call the parts of the date with `lubridate', and divide by the hours in the day, where here they are 24 rather than 7.
现在,您需要将数据缩放到只有您想要的时间。您可以使用`lubridate'调用日期的各个部分,然后除以当天的小时数,这里它们是24而不是7。
hour(dates) <- hour(dates)-10
scaleddates <- day(dates)-1 + hour(dates)/7 + minute(dates)/60/7 + second(dates)/60/60/7
scaleddates
[1] 0.0000000 0.4285714 1.0000000 1.4285714 2.0000000
Now you can plot the graph, with the x-axis reading 'Days' rather than 'Dates'. The x-axis is now how far you are through the working day.
现在您可以绘制图形,x轴读数为“Days”而不是“Dates”。 x轴现在是你工作日的距离。