简体   繁体   English

当格式为日期时,如何不在x轴上显示所有值? ggplot R

[英]How to not display all values on x axis when format is date? ggplot R

I have a data frame as follows. 我有一个数据框如下。

TIME                 PRICE
2013-01-01 23:55:03 446.6167
2013-01-01 23:55:02 441.0114
2013-01-01 23:54:59 446.7600

I want to plot the following as a scatter plot in R using ggplot. 我想使用ggplot将以下内容绘制为R中的散点图。 The times are unique. 时代是独一无二的。

I want to plot the data showing x labels only once in every 30 minutes. 我只想每30分钟绘制一次显示x标签的数据。 Can someone help me? 有人能帮我吗?

class(inter_1$TRANSACTION_TIME)
[1] "factor"

Thanks in advance! 提前致谢!

You need to convert your "TIME" column to a "time" format, since now it is a factor. 您需要将“时间”列转换为“时间”格式,因为现在这是一个因素。 Something like this would work: 这样的事情会起作用:

library(anytime)
library(dplyr)
mydf %>% 
  mutate(TIME = anytime(TIME)) %>% 
  as_tibble()

## # A tibble: 2 x 2
##                  TIME    PRICE
##                <dttm>    <dbl>
## 1 2013-01-01 22:55:03 446.6167
## 2 2013-01-01 22:55:02 441.0114

You see that now "TIME" is a "date-time" variable. 您会看到现在“ TIME”是一个“ date-time”变量。 Then, you can adjust the x-axis using scale_x_date_time . 然后,您可以使用scale_x_date_time调整x轴。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM