0
votes

I am getting error while plotting x axis scale on ggplot.

lubridate::parse_date_time(df1$hr,"H:M") -> df1$hr

ggplot(data = df1 , aes(x = hr, y = Val, color = Date)) +geom_line() +
 scale_x_datetime(date_breaks = "1 hours", date_labels = "%H:%M")

Below is my sample data.

df1 <- data.table::fread("Date  hr  Val
19/02/19    0:00    1292
19/02/19    1:00    1047
19/02/19    2:00    160
19/02/19    3:00    80
19/02/19    4:00    67
19/02/19    5:00    48
19/02/19    6:00    30
19/02/19    7:00    99
19/02/19    8:00    188
19/02/19    9:00    42
19/02/19    10:00   14
19/02/19    11:00   27
19/02/19    12:00   21
19/02/19    13:00   21
19/02/19    14:00   110
19/02/19    15:00   535
19/02/19    16:00   1503
19/02/19    17:00   2626
19/02/19    18:00   4859
19/02/19    19:00   5699
19/02/19    20:00   5718
19/02/19    21:00   5337
19/02/19    22:00   6101
19/02/19    23:00   5381
20/02/19    0:00    1836
20/02/19    1:00    236
20/02/19    2:00    96
20/02/19    3:00    79
20/02/19    4:00    115
20/02/19    5:00    123
20/02/19    6:00    137
20/02/19    7:00    142
20/02/19    8:00    119
20/02/19    9:00    99
20/02/19    10:00   92
20/02/19    11:00   109
20/02/19    12:00   118
20/02/19    13:00   159
20/02/19    14:00   269
20/02/19    15:00   648
20/02/19    16:00   981
20/02/19    17:00   1371
20/02/19    18:00   1804
20/02/19    19:00   2772
20/02/19    20:00   4582
20/02/19    21:00   5346
20/02/19    22:00   5244
20/02/19    23:00   4956")

what I am trying to do is to plot all hours on x axis scale but getting below error

Error in as.POSIXlt.character(as.character(x), ...) :     character string 
is not in a standard unambiguous format. 

However it works for subset of same data frame.

I think it works when hour is either 1 digit long or two digit long and not both at same time

1
Its there. Just scroll down .. ggplot(data = df1 , aes(x = hr, y = Val, color = Date)) +geom_line()+ scale_x_datetime(date_breaks = "1 hours", date_labels = "%H:%M")Aprilian8
Check what comes out of parse_date_time - that's not a proper date time format.markus
What do you want to plot? Different line for each day?Jakub Buček
yes. different line for each date. and only hour on x axisAprilian8
It resolved by changing line > df1$hr<-as.POSIXct(df1$hr,format="%H") > ggplot(data = df1 , aes(x = hr, y = Val, color = Date)) +geom_line()+ + scale_x_datetime(date_labels = "%H",date_breaks = "1 hour")Aprilian8

1 Answers

1
votes

I would do it this way:

library(lubridate)

#get df1...

#convert hr to something more useable with...
df1$hr <- hour(hm(df1$hr))

ggplot(df1, aes(x = hr, y = Val, color = Date)) + geom_line()

Graph of Val vs Time of Day grouped by Date

or if you specifically want "%H:%M" or similar as your x-axis label time format:

ggplot(df1, aes(x = hr, y = Val, color = Date)) + geom_line() + scale_x_continuous(breaks = seq(0, 23, 1), labels = paste(seq(0, 23, 1), "00", sep = ":"))

Graph of Val vs Time of Day grouped by Date with x labels as hour:minute

Explanation: I never find working with times and dates easy, so if you can simplify the problem a little, do it.

In your case you are just interested in the hour of day so minutes are superfluous. To extract just the hour from your times as a character data type, there are a few ways, but using lubridate, you could use the hm(df1$hr) which will convert "10:00" to "10H 0M 0S" which is a period data type, from which you just want the hour which you can do with hour() which will give you 10 as a numeric data type.

So as a one liner hour(hm(df1$hr)) will convert "10:00" to 10, then ggplot can just plot "Val" vs "hr" both as numeric data types instead of time or character data types which I think is an easier task.