简体   繁体   中英

How to change the x-values displayed in ggplot and ggplotly in R

I have a timeseries X, with related timestamps, and i want to provide a graph with the hourly values. The X-axis should not show the timestamp but rather the hour, however the plot should be generated with the timestamp as the x-axis

I've already tried to plot the X with the related timestamps (hours from 0-23) using the scale_x_datetime function. However the problem arises when trying to get the x-values to show 1-24. A problem arises when you go over midnight and end up with duplicated x-values

Timestamp <- c("2019-07-30 23:00:00", "2019-07-31 00:00:00", "2019-07-31 01:00:00", "2019-07-31 02:00:00", "2019-07-31 03:00:00", "2019-07-31 04:00:00", "2019-07-31 05:00:00", "2019-07-31 06:00:00",
               "2019-07-31 07:00:00", "2019-07-31 08:00:00", "2019-07-31 09:00:00", "2019-07-31 10:00:00", "2019-07-31 11:00:00", "2019-07-31 12:00:00",
               "2019-07-31 13:00:00", "2019-07-31 14:00:00", "2019-07-31 15:00:00", "2019-07-31 16:00:00", "2019-07-31 17:00:00", "2019-07-31 18:00:00",
               "2019-07-31 19:00:00", "2019-07-31 20:00:00", "2019-07-31 21:00:00", "2019-07-31 22:00:00", "2019-07-31 23:00:00", "2019-08-01 00:00:00","2019-08-01 01:00:00")
col <- c(110,100,105,100,105,100,110,100,110,100,110,100,110,100,110,100,
       110,100,110,100,110,100,110,105,110,105,110)
hour <- c(23,24,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,1)
Timestamp <- as.POSIXct(Timestamp, tz = "GMT")

library(plotly)
library(lubridate)
library(scales)
library(ggplot2)

data <- data.frame(Timestamp,col,hour)

data$Timestamp <- as.POSIXct(data$Timestamp)
ggplot(data = data, aes(x = Timestamp))+
    geom_line(aes(y = col))+
    scale_x_datetime(date_breaks = "1 hours", 
                   date_minor_breaks = "1 hour",
                   labels = date_format("%H"))

The desired output is a ggplot/ggplotly with the hours displayed as the x-axis (1-24 and not 0-23).

Future work would also a timeseries with 1 minute resolution to be included in the plot

edited: added working code. I want the x-axis to be 1-24 and start over at 1 after 24

Here i found the answer. It was an evolution and addition to the scale_x_datetime function

Timestamp <- c("2019-07-30 23:00:00", "2019-07-31 00:00:00", "2019-07-31 01:00:00", "2019-07-31 02:00:00", "2019-07-31 03:00:00", "2019-07-31 04:00:00", "2019-07-31 05:00:00", "2019-07-31 06:00:00",
               "2019-07-31 07:00:00", "2019-07-31 08:00:00", "2019-07-31 09:00:00", "2019-07-31 10:00:00", "2019-07-31 11:00:00", "2019-07-31 12:00:00",
               "2019-07-31 13:00:00", "2019-07-31 14:00:00", "2019-07-31 15:00:00", "2019-07-31 16:00:00", "2019-07-31 17:00:00", "2019-07-31 18:00:00",
               "2019-07-31 19:00:00", "2019-07-31 20:00:00", "2019-07-31 21:00:00", "2019-07-31 22:00:00", "2019-07-31 23:00:00", "2019-08-01 00:00:00","2019-08-01 01:00:00")
col <- c(110,100,105,100,105,100,110,100,110,100,110,100,110,100,110,100,
         110,100,110,100,110,100,110,105,110,105,110)
hour <- c(23,24,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,1)
Timestamp <- as.POSIXct(Timestamp, tz = "GMT")

library(plotly)
library(lubridate)
library(scales)
library(ggplot2)

data <- data.frame(Timestamp,col,hour)

data$Timestamp <- as.POSIXct(data$Timestamp)
ggplot(data = data, aes(x = Timestamp))+
  geom_line(aes(y = col))+
  scale_x_datetime(date_breaks = "1 hours", 
                   date_minor_breaks = "1 hour",
                   labels = function(x) ifelse(hour(as.POSIXct(x, origin = '1970-01-01'))==0, 24, hour(as.POSIXct(x, origin = '1970-01-01'))))


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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