Plotly R:无法使自定义 xaxis 日期范围起作用

编程入门 行业动态 更新时间:2024-10-10 01:27:46
本文介绍了Plotly R:无法使自定义 xaxis 日期范围起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这应该很简单(我认为),但由于某种原因我无法让它工作.我有这样的数据:

This should be very simple (I think) and yet I can't get this to work for some reason. I have data like this:

df x y 1 2016-08-12 1 2 2016-08-13 2 3 2016-08-14 3 4 2016-08-15 4 5 2016-08-16 5 str(df) 'data.frame': 5 obs. of 2 variables: $ x: Date, format: "2016-08-12" "2016-08-13" ... $ y: int 1 2 3 4 5

我使用 plotly 如下,但它不会将 xaxis 范围调整为我指定的自定义值.相反,使用数据的范围:

I am using plotly as follows, but it does not adjust the xaxis range to my specified custom values. Instead, uses the range of the data:

library(plotly) plot_ly(df, x = x, y = y) %>% layout(xaxis = list(range = c(as.Date('2016-08-01'), as.Date('2016-08-31'))))

另一方面,如果我有这样的数字数据:

On the other hand, if I have numeric data like this:

df x y 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

并像这样指定一个自定义范围,它工作正常:

And specify a custom range like this, it works fine:

plot_ly(df, x = x, y = y) %>% layout(xaxis = list(range = c(-5, 10)))

在使数据范围起作用时我缺少什么?

What am I missing in making data ranges work?

推荐答案

引用自 plot.ly/r/reference/#layout-xaxis-range(我加粗):

range (list) 设置此轴的范围.如果轴 type 是日志",那么您必须记录所需范围的日志(例如设置范围从 1 到 100,设置范围从 0 到 2).如果轴type 是日期",那么你必须将日期转换为unix时间毫秒(自 1970 年 1 月 1 日以来的毫秒数).为了例如,要设置从 1970 年 1 月 1 日到 11 月 4 日的日期范围,2013,设置范围从0到1380844800000.0每个命名列表都有一个或更多下面列出的键.

range (list) Sets the range of this axis. If the axis type is "log", then you must take the log of your desired range (e.g. to set the range from 1 to 100, set the range from 0 to 2). If the axis type is "date", then you must convert the date to unix time in milliseconds (the number of milliseconds since January 1st, 1970). For example, to set the date range from January 1st 1970 to November 4th, 2013, set the range from 0 to 1380844800000.0 Each named list has one or more of the keys listed below.

因此,您可以将日期转换为以毫秒为单位的 unix 时间:

So, you can convert your dates to the unix time in milliseconds:

plot_ly(df, x = x, y = y) %>% layout(xaxis = list( range = c(as.numeric(as.POSIXct("2016-08-01", format="%Y-%m-%d"))*1000, as.numeric(as.POSIXct("2016-08-31", format="%Y-%m-%d"))*1000), type = "date"))

更多推荐

Plotly R:无法使自定义 xaxis 日期范围起作用

本文发布于:2023-07-19 13:47:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   日期   Plotly   xaxis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!