Reverse datetime (POSIXct data) axis in ggplot version 3+
Is this what you're trying to achieve?
y-scale as time, earliest time at the bottom:
ggplot(myData, aes(y=value, x=timestamp)) +
geom_path() +
scale_x_datetime() +
coord_flip()
The code posted in the question works for me. Is this the desired output:
Reverse datetime (POSIXct data) axis in ggplot
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:
How to reverse order of x-axis breaks in ggplot?
This is exactly what scale_x_reverse
is for:
ggplot(test2, aes(Freq, SD, z = Intro_0)) +
geom_contour_filled(bins = 9)+
scale_fill_brewer(palette = "BuPu")+
labs(x = "Frequency", y = "Magnitude", title = "Test Plot", fill = "Legend") +
scale_x_reverse(breaks = c(1, 2, 3, 5, 10, 15, 20),
labels = c("Freq = 1/1", "", "", "", "", "", "Freq = 1/20")) +
theme_bw()+
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.x = element_text(angle = 45, hjust=1),
plot.caption.position = "plot",
plot.caption = element_text(hjust = 0))
In R, how can I extend a posixct timeseries axis beyond the end of the data
You can use the limits
argument within scale_x_datetime
to expand the scale.
library(dplyr)
library(ggplot2)
library(scales)
library(lubridate)
# Example data
df <- data.frame(date = as.POSIXct(
c(
"2016-12-05-16.25.54.875000",
"2016-12-06-15.25.54.875000",
"2016-12-08-14.25.54.875000",
"2016-12-09-13.25.54.875000",
"2016-12-09-12.25.54.875000",
"2016-12-10-11.25.54.875000",
"2016-12-10-10.25.54.875000",
"2016-12-11-07.25.54.875000"
),
format = "%Y-%m-%d-%H.%M.%S"
) ,
y = 1:8)
Default axis limits
The minimum and maximum values of date
are the default limits, even if there is no label or tick mark at the spot.
ggplot(df, aes(x = date, y = y)) +
geom_point() +
scale_x_datetime(labels = date_format("%D"),
date_breaks = "2 day")
Expanded axis limits
We can expand the axis limits even to values not observed in our data. Once again, you'll need to adjust labels and tick marks if you want to scale them the same as well.
ggplot(df, aes(x = date, y = y)) +
geom_point() +
scale_x_datetime(
labels = date_format("%D"),
date_breaks = "2 day",
limits = ymd_h("2016-12-05 00", "2016-12-20 23")
)
Reversing the y axis on discrete data
Use:
ylim("20220427", "20220420", "20220413")
Reverse x-axis that contains categorical data and a lot of annotations
We could make annotation labels and shading part of the input data. Then annotations will reverse together with x-axis ordering. Something like:
library(tidyverse)
# dummy data
mtcars %>%
mutate(make = word(rownames(mtcars))) %>%
group_by(make) %>%
summarize(wt = sum(wt)) %>%
head ->
mt
# Option to reverse, choose one
# if it is a function, pass an argument
# foo <- function(data, myReverseOption = FALSE, ...
myReverseOption = TRUE
myReverseOption = FALSE
mt$make <- as.factor(mt$make)
if(myReverseOption){
mt$make <- factor(mt$make, levels = rev(levels(mt$make))) }
# add annotaions
mt <- mt %>%
mutate(
myLabel = if_else(make == "Camaro", "OK, y'all?", NA_character_),
myShade = grepl("^C", make))
# plot
ggplot(mt, aes(x = make, y = wt)) +
geom_bar(stat = "identity") +
geom_text(aes(label = myLabel), nudge_y = 1) +
geom_rect(aes(xmin = (as.numeric(make) - 0.5) * myShade,
xmax = (as.numeric(make) + 0.5) * myShade,
ymin = 4, ymax = 6),
alpha = 0.5) +
ggtitle(ifelse(myReverseOption, "reversed", "original"))
Related Topics
Lme4::Lmer Reports "Fixed-Effect Model Matrix Is Rank Deficient", Do I Need a Fix and How To
How to Write to JSON with Children from R
How to Change Order of Array Dimensions
Convert Character Matrix into Numeric Matrix
Finding the Index Inside a Vector Satisfying a Condition
Rounding Numbers in R to Specified Number of Digits
What Is About the First Column in R's Dataset Mtcars
Create Counter of Consecutive Runs of a Certain Value
Is There Anything Wrong with Using T & F Instead of True & False
Using R Statistics Add a Group Sum to Each Row
Plot a Function with Ggplot, Equivalent of Curve()
How to Use Map from Purrr with Dplyr::Mutate to Create Multiple New Columns Based on Column Pairs
Control the Height in Fluidrow in R Shiny