简体   繁体   中英

R ggplot sorting issue

In a data frame, I have time of day as a string, so it can be anything from 23:59 to 0.00 (highest to lowest as imported). ggplot somehow does a sort on that column and plots "time" in a numeric order. Eg, 0:00, then 11:15, then 22:00, then 3:15, 4:30, etc.

I have tried the various methods of reordering but nothing seems to get the time as I would expect it, as in 0:00 to 23:59. I would like to plot the data as a histogram according to time but ggplot just doesn't like the time values.

Any guidance would be most welcome.

Output of newdf <- hm(df$Time)

> str(newdf)
Formal class 'Period' [package "lubridate"] with 6 slots
  ..@ .Data : num [1:39] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ year  : num [1:39] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ month : num [1:39] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ day   : num [1:39] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ hour  : num [1:39] 17 16 16 16 16 15 15 15 15 14 ...
  ..@ minute: num [1:39] 0 45 30 15 0 45 30 15 0 45 ...

Easiest workaround would be to convert it into a decimal:

times<-as.numeric(gsub(":",".",times))

Now it will sort it in the right order.

times<-c("0:00","11:35","3:45","22:30")
sort(as.numeric(gsub(":",".",times)))
[1]  0.00  3.45 11.35 22.30

Note that this is not a GOOD workaround, as you will end up with weird gaps between times, because the distance between 1:45 and 2:00 is NOT the same as the one between 1.45 and 2.00. So if you're planning on displaying this on an interval axis, rather than just a nominal one, you'll need to convert into a date time class. Or, actually you could use hm() from the lubridate package:

library(lubridate)
sort(hm(times))
[1] "0S"         "3H 45M 0S"  "11H 35M 0S" "22H 30M 0S"

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