Reverse datetime (POSIXct data) axis in ggplot

后端 未结 1 1654
终归单人心
终归单人心 2020-12-03 08:18

I\'m trying to create a plot of POSIXct times using ggplot, and would like to reverse the axis, but am struggling to make it work. I\'ve been using scale_y_datetime

相关标签:
1条回答
  • 2020-12-03 09:07

    With the help from this post from Hadley Wickham here is how you can get a reverse datetime scale:

    c_trans <- function(a, b, breaks = b$breaks, format = b$format) {
      a <- as.trans(a)
      b <- as.trans(b)
    
      name <- paste(a$name, b$name, sep = "-")
    
      trans <- function(x) a$trans(b$trans(x))
      inv <- function(x) b$inverse(a$inverse(x))
    
      trans_new(name, trans, inverse = inv, breaks = breaks, format=format)
    
    }
    
    rev_date <- c_trans("reverse", "time")
    
    ggplot(MyData, aes(x=Value, y=Date)) +
      geom_point() + 
      scale_y_continuous(trans = rev_date)
    

    Here is the plot:

    0 讨论(0)
提交回复
热议问题