Home > Software design >  How do you spread out a specific part of the x-axis in ggplot2?
How do you spread out a specific part of the x-axis in ggplot2?

Time:11-16

I have a dataframe:

df <- data.frame(tw = rep(c(1:30), each = 3),
                 name = rep(c("spec1", "spec2", "spec3"), each = 1),
                 id = rep(c(1:10), each = 90),
                 value = sample(x = 0:100, size = 900, replace = TRUE))

Where tw (time window) represents units of time, but they are not evenly spaced. From tw 1:25 is every 500 years, whereas from tw 26 - 30 is every ~ 1500 years.

At the moment I'm plotting it like this:

library(ggplot2)
ggplot()   
  geom_line(data=df, aes(x=tw, y=value, group=interaction(id, name), colour=name),  alpha = .15)  
  scale_x_reverse(breaks = scales::pretty_breaks(n = 15))  
  scale_colour_manual(values = c("#ffa644", "#30460a", "#E64056"))  
  geom_vline(xintercept = 25, )  
  ylab("Percentage (%)")   
  xlab("Time")  
  labs(colour = "Taxa")   
  theme_bw()

I'd like tws 26:30 (before the intercept line) to be spaced further apart than 1:25 along the x axis, to represent the longer time gap. I've tried to draw an example below.

enter image description here

  • Related