微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R x 轴毫秒到日期标签

如何解决R x 轴毫秒到日期标签

所以我有一组数据,其中包含每个条目的时间戳(以毫秒为单位)。 示例数据:

"date","lowest_price","median_price","volume"
"1615219740000","1.61€","13.785"
"1615322760000","1.46€","1.51€","12.496"
"1615322760000","12.496"
"1615322820000","12.496"
"1615322940000","12.496"
"1615323540000","1.49€","12.496"
"1615324140000","12.496"
"1615326000000","1.50€","1.45€","12.413"

我现在想使用数值进行计算/绘图,但对于可视化本身,我想要一个将日期显示为时间戳的间隔(例如 14-03-2021 14:00)

目前我拥有的是:

pp <- ggplot(data=breakout_case,aes(x = date,y = lowest_price,group = 1)) +
  geom_line() + 
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Operation Breakout Case Price-chart",x = "Date",y = "Price"
        )
pp

它看起来像这样:

enter image description here

所以我需要例如 1.61e+12 等而不是14-03-2021 14:00(不是正确的转换,仅作为示例)

我也不太清楚,为什么 x 轴上已经有一个刻度。

提前致谢

解决方法

转换为 posixCT,然后使用 scale_x_datetime-properties 格式化 x 轴

使用的样本数据

mydata <- read.table( text = ' "date","lowest_price","median_price","volume"
                      "1615219740000","1.61€","13.785"
                      "1615322760000","1.46€","1.51€","12.496"
                      "1615322760000","12.496"
                      "1615322820000","12.496"
                      "1615322940000","12.496"
                      "1615323540000","1.49€","12.496"
                      "1615324140000","12.496"
                      "1615326000000","1.50€","1.45€","12.413" ',sep = ",",header = TRUE)

代码

library( tidyverse )
library( scales )
mydata %>%
  dplyr::mutate( timestamp = date %>% 
                   as.numeric %>% #make it numeric
                   `/`(1000) %>%  #divide by 1000
                   as.POSIXct( origin = "1970-01-01" ) ) %>% #set to POSIXct
  ggplot( aes( x = timestamp,y = lowest_price,group = 1 ) ) + 
  geom_line() +
  #set axis properties here
  scale_x_datetime( breaks = "3 hour",labels = scales::date_format( "%Y-%m-%d %H:%M" ) ) +
  #rotete x-labels
  theme( axis.text.x = element_text( angle = 90,vjust = 0.5 ) )

输出

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?