Creating a break (//) in my x-axis (date)

Hello! Here is an example code of the current data I am working with and my problem. I am trying to add a break into my x-axis with / /, currently using ggplot but if other packages work then that's great! I tried using plotrix and also using ggbreak and got the closest to my desired output with ggbreak but I would like the clean // break in my graph. This is probably due to my data for the x-axis being dates in POSIXct format as a lot of these packages are for continuous values. Please let me know if you have a solution for me, thank you!

Here is my code (apologies for the long set up for sample data):

# library -----------------------------------------------------------------
library(here)
library(tidyverse)
library(reprex)
library(ggplot2)
library(ggbreak) 

# Create continuous data -----------------------------------------------------------------
x1 <- seq(0, 10, length.out = 100) 
y1 <- sin(x1)

# Create a gap in the data 
x_gap <- seq(10, 50, length.out = 1) 
y_gap <- rep(NA, length(x_gap)) 

# Create more continuous data after the gap
x2 <- seq(50, 60, length.out = 100) 
y2 <- cos(x2)  

# Combine all parts into a data frame
x <- c(x1, x_gap, x2)
y <- c(y1, y_gap, y2) 

# x-values to dates 
base_date <- as.Date("2025-03-17")
dates <- base_date + x

# Creating the data frame
df <- data.frame(date = dates, y = y)

# Plot the data  -----------------------------------------------------------------
p1 <- ggplot(df, aes(x = date, y = y)) +
  geom_line(color = "blue") +
  geom_smooth() + 
  labs(title = "Plot of Continuous Data with a Gap", x = "X", y = "Y") +
  theme_minimal() +
  theme(panel.grid.major = element_line(color = "gray", size = 0.2), panel.grid.minor = element_blank())

p1 + scale_x_break(c(20, 51))

plot(df$date, df$y, type = "l", col = "blue", main = "Plot with a Gap", xlab = "Time", ylab = "Value")

And here is an example of the type of break I am looking for
image

For your example illustration, where you have discrete data, one solution would be to convert your continuous x variable Month to a categorical one:
Month <- as.factor(Month)

But then I ran your code and see you have continuous numeric data with a break. I don't have a solution to that.

1 Like

Another option is to create a var with a value for 1:3 and another value for 10:12 - and then facet by that variable. You can remove the strip labels if you want

1 Like

Staying with continuous data, you can get the breaks at the correct location if they're in the right unit:

p1 +
  scale_x_break(as.Date(c("2025-04-01", "2025-05-01")))

Unfortunately, this converts the labels to numeric. But, scales_x_break() can be combined with other scales! So we can add a scale_x_continuous() on top of it to force relabelling:

p1 +
  scale_x_break(as.Date(c("2025-04-01", "2025-05-01"))) +
  scale_x_continuous()

This is not enough: it just adds a second numeric scale in a weird way I can't explain. But, if we specify the breaks ourselves:

p1 +
  scale_x_break(as.Date(c("2025-04-01", "2025-05-01"))) +
  scale_x_continuous( breaks = scales::breaks_pretty(n = 10)(df$date) )

There is still a double scale on top, no idea why, but at least they all display correct dates.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.