Reversing scale_datetime (scale_y_datetime)

928 views
Skip to first unread message

Emiel van de Laar

unread,
Jul 18, 2011, 3:14:55 AM7/18/11
to ggp...@googlegroups.com
Hi,

I have a scatter plot with a datetime scale (POSIXct type) for the y axis. I would like
the dates to read down (earlier down to later) but have yet to figure out how to do this.

 * There is no 'trans' parameter for scale_datetime.
 * Reversing the 'limits' does not seem to work either.

Thanks!

 - Emiel
--

library(ggplot2)

df <- data.frame(
        ts = as.POSIXct(seq(1, 10), origin="1970-01-01"),
        data = rnorm(10))

p <- ggplot(df)
p <- p + aes(x=data, y=ts)
p <- p + geom_point()
p <- p + scale_y_datetime(name='Timestamp', format='%H:%M:%S')

ggsave(filename='sample.pdf')

dev.off()

Hadley Wickham

unread,
Jul 19, 2011, 5:30:00 PM7/19/11
to ggp...@googlegroups.com
Hmmm, this is a little tricky, because you need to combine two
transformers: date time and reverse. Here's some ideas for the next
version of ggplot2, but unfortunately I don't have enough right time
to get it completely working. Hopefully it's enough of a start for
someone else to figure out:

library(scales)
c_trans <- function(a, b, breaks = a$breaks, format = a$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, inv, breaks, format)
}

rev_date <- c_trans("reverse", "time")

scale_y_continuous(trans = rev_date)

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages