r - 如何为 highcharter columnrange 图使用日期范围?
问题描述
我正在尝试使用 Highcharter 创建一个列范围图,其中列表示日期范围。虽然这适用于整数,但不适用于日期。其他情节类型似乎可以很好地处理日期。
以天气 df 为例:
weather2 <- weather %>%
filter(date < mdy("03-31-2014"))%>%
mutate(low = date, high=date+30, id = row_number()) %>%
select(id, low, high)
hchart(weather2,
type = "columnrange",
hcaes(x = id, high=high, low=low))
该图只是呈现为空白,只有 x 轴标签。
但是,如果我只是将日期转换为整数,它会按预期显示一个范围。
weather3 <- weather %>%
filter(date < mdy("03-31-2014"))%>%
mutate(low = as.integer(date), high=as.integer(date+30), id = row_number()) %>%
select(id, low, high)
hchart(weather2,
type = "columnrange",
hcaes(x = id, high=high, low=low))
我也尝试过添加%>% hc_yAxis(type = "datetime")
,但这也无济于事。
谢谢!
解决方案
似乎 columnrange 绘图类型要求日期为时间戳格式。它们可以使用datetime_to_timestamp
highcharter 中的函数进行转换,然后使用hc_yAxis(type = "datetime")
.
weather2 <- weather %>%
filter(date < mdy("03-31-2014"))%>%
mutate(low = datetime_to_timestamp(date),
high = datetime_to_timestamp(date+30),
id = row_number()) %>%
select(id, low, high)
hchart(weather2,
type = "columnrange",
hcaes(x = id, high=high, low=low)) %>%
hc_yAxis(type = "datetime")
推荐阅读
- python - Tkinter 按钮冻结
- flutter - 如何在 Flutter 中为 TextFormField 设置填充材料设计风格?
- laravel - 如何修复 SQLSTATE [42704]:未定义对象:7 错误:数据类型 json 在 heroku 托管中没有用于访问方法“btree”的默认运算符类
- javascript - 使用 Ajax 在 jQuery 按钮中添加对 PHP 的调用
- flutter - 相机预览在颤动中拉伸
- java - 使用Java中的列表方法查找边界之间的素数
- javascript - 使用 Discord.js 对获取的消息做出反应
- javascript - 如何检查amcharts地图上的点(经度,纬度坐标)是否在某个地区(县)?
- html - 如何调整 CSS Grid 区域的大小以堆叠在小屏幕上?
- javascript - 输入一个单词后,光标跳到该单词的开头