如何在平滑散点图中正确设置日期字段的格式?

发布于 2024-11-29 18:17:20 字数 1414 浏览 1 评论 0原文

我的数据如下所示:

> head(data)
                 date    price volume
1 2011-06-26 17:16:05 17.51001  2.000
2 2011-06-26 20:50:00 14.80351  2.981
3 2011-06-26 20:51:00 14.90000  2.000
4 2011-06-26 20:52:00 14.89001  0.790
5 2011-06-26 20:53:00 15.00000  1.000
6 2011-06-26 21:05:01 16.20000  6.500
> str(head(data))
'data.frame':   6 obs. of  3 variables:
 $ date  : POSIXct, format: "2011-06-26 17:16:05" "2011-06-26 20:50:00" "2011-06-26 20:51:00" "2011-06-26 20:52:00" ...
 $ price : num  17.5 14.8 14.9 14.9 15 ...
 $ volume: num  2 2.98 2 0.79 1 ...

当我像这样绘制它时:

someColors <- colorRampPalette(c("black", "blue", "orange", "red"), space="Lab")
smoothScatter(data, colramp=someColors)

我几乎得到了我正在寻找的内容,但它将 posix 日期转换为数字。如何更有效地设置 x 标签,以便我的内容更具可读性?

https: //img.skitch.com/20110812-nmsrtdbtaey3yqa29g8xkc17tn.png
(来源:skitch.com

编辑:我可以像这样得到我想要的近似值:

smoothScatter(data, colramp=someColors, xaxt="n")
axis(1, at=data$date,
     labels=lapply(data$date, function(d) strftime(d, "%F")),
     tick=FALSE)

不过,这非常慢。看来我应该能够准备数据或为标签抽屉提供一些建议。

I have data that looks like this:

> head(data)
                 date    price volume
1 2011-06-26 17:16:05 17.51001  2.000
2 2011-06-26 20:50:00 14.80351  2.981
3 2011-06-26 20:51:00 14.90000  2.000
4 2011-06-26 20:52:00 14.89001  0.790
5 2011-06-26 20:53:00 15.00000  1.000
6 2011-06-26 21:05:01 16.20000  6.500
> str(head(data))
'data.frame':   6 obs. of  3 variables:
 $ date  : POSIXct, format: "2011-06-26 17:16:05" "2011-06-26 20:50:00" "2011-06-26 20:51:00" "2011-06-26 20:52:00" ...
 $ price : num  17.5 14.8 14.9 14.9 15 ...
 $ volume: num  2 2.98 2 0.79 1 ...

When I plot it like this:

someColors <- colorRampPalette(c("black", "blue", "orange", "red"), space="Lab")
smoothScatter(data, colramp=someColors)

I get almost exactly what I'm looking for, but it converts the posix dates to numbers. How can I set the x labels more usefully so that my stuff is a bit more readable?

https://img.skitch.com/20110812-nmsrtdbtaey3yqa29g8xkc17tn.png
(source: skitch.com)

Edit: I can get an approximation of what I want like this:

smoothScatter(data, colramp=someColors, xaxt="n")
axis(1, at=data$date,
     labels=lapply(data$date, function(d) strftime(d, "%F")),
     tick=FALSE)

That's terribly slow, though. It seems like I should be able to prep the data or advice the label drawer a bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2024-12-06 18:17:20

就速度而言,指定用于 x 轴标签的日期范围可能会有所帮助。例如:

days <- seq(min(data$date), max(data$date), by = 'month')
axis(1, at=days,
     labels=strftime(days, "%F"),
     tick=FALSE)

将时间四舍五入到最近的一天也可能有所帮助:

days <- seq(as.Date(min(data$date)), as.Date(max(data$date)), by = 'month')

In terms of speed, it might help to specify range of dates to use for the x-axis labels. For example:

days <- seq(min(data$date), max(data$date), by = 'month')
axis(1, at=days,
     labels=strftime(days, "%F"),
     tick=FALSE)

It might also help to round the times to the nearest day:

days <- seq(as.Date(min(data$date)), as.Date(max(data$date)), by = 'month')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文