I'm trying to reverse the y-axis of a plot. Now the column is of a class date
. Numeric columns can be reversed by adding scale_y_reverse()
or scale_y_continuous(trans = "reverse)
but I can't seem to figure out how to get from top to bottom: 2005, 2006, 2007
. I can't convert the date column to numeric because I've annotations layers on months in my original plot.
library(tidyverse)
df <- structure(list(date = structure(c(12784, 13149, 13514), class = "Date"),
nr = c(1.14192497730255, 0.719137012958527, 1.3783597946167
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-3L), .Names = c("date", "nr"))
# A tibble: 3 x 2
date nr
<date> <dbl>
1 2005-01-01 1.14
2 2006-01-01 0.719
3 2007-01-01 1.38
Plot:
df %>%
ggplot(aes(date, nr)) +
geom_col() +
coord_flip() +
scale_x_date(date_labels = "%Y",
date_breaks = "1 years")
Edit
I also can't get this answer to work because my column is of class date
and not POSIXct
:
Error: Invalid input: time_trans works with objects of class POSIXct only