0

I have the following data, which produces the following two plots stacked on top of each other:

#dataset 1
channel1 <- c("Channel 1", "Channel 1", "Channel 1", "Channel 1", "Channel 1")
start_time1 <- c(0.000, 9.719, 11.735, 14.183, 16.554)
stop_time1 <- c(9.719, 11.735, 14.183, 16.554, 18.482)
character1 <- c("A", "B", "C", "C", "B")

df1 <- data.frame(channel1, start_time1, stop_time1, character1)

#dataset1 plot
plot1 <- ggplot(df1, aes(fill = character1, color = character1)) +
  geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
  #xlim(0,20) +
  facet_grid(channel1 ~ .) +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank() ) +
  scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF"))


#dataset 2
channel2 <- c("Channel 2", "Channel 2", "Channel 2", "Channel 2", "Channel 2")
start_time2 <- c(3, 3, 4, 7, 9)
stop_time2 <- c(3.5, 4, 6, 8, 11)
character2 <- c("A", "D", "C", "E", "B")

df2 <- data.frame(channel2, start_time2, stop_time2, character2)

#dataset2 plot

plot2 <- ggplot(df2, aes(fill = character2, color = character2)) +
  geom_rect(color = NA, aes(xmin = start_time2, xmax = stop_time2, ymin = -0.5, ymax = 0.5)) +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
  #xlim(0, 20) +
  facet_grid(channel2 ~ .) +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank() ) +
  scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF"))

#combine plots
library(patchwork)

continuous

This is not good, because I want them to have same x-axis scale, which I can do by by using xlim instead of using scale_x_continuous, which produces plots that are more visually comparable:

#dataset 1
channel1 <- c("Channel 1", "Channel 1", "Channel 1", "Channel 1", "Channel 1")
start_time1 <- c(0.000, 9.719, 11.735, 14.183, 16.554)
stop_time1 <- c(9.719, 11.735, 14.183, 16.554, 18.482)
character1 <- c("A", "B", "C", "C", "B")

df1 <- data.frame(channel1, start_time1, stop_time1, character1)

#dataset1 plot
plot1 <- ggplot(df1, aes(fill = character1, color = character1)) +
  geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) +
  #scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
  xlim(0,20) +
  facet_grid(channel1 ~ .) +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank() ) +
  scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF"))

xlim

But what if I want to customize the x-axis breaks? What if I want a break at every multiple of 2 instead of 5? I've looked around a lot, and haven't yet found a way to customize the breaks when the x-axis limit is fixed. scale_x_discrete() doesn't seem to do anything useful in this situation.

Any suggestions are most appreciated.

2
  • You can use breaks to scale it however you would like, for example, every 2 can be accomplished with scale_x_continuous(breaks = seq(0, 20, by = 2)). Commented Dec 4, 2023 at 1:07
  • that did change the breaks to multiples of two, the x-axis scales were still different Commented Dec 4, 2023 at 4:05

1 Answer 1

1

Use scale_x_continuous() to specify breaks, and coord_cartesian(xlim = c(0, 20)) to define x limits for both plots.

plot1 <- ggplot(df1, aes(fill = character1, color = character1)) +
  geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) +
  scale_x_continuous(breaks = seq(0, 20, 2)) +
  coord_cartesian(xlim = c(0, 20)) +
  facet_grid(channel1 ~ .) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_manual(values = c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF"))

Sign up to request clarification or add additional context in comments.

3 Comments

for this specific example, yes that would work, and this is what I did originally, but, I have reasons to not do it this way, you can see another question I have here: stackoverflow.com/questions/77588625/… wherein I want to treat different sets of data differently with their own legends, thus warranting making each of them a separate plot
Then, the solution would be the same: use scale_x_continuous(breaks = seq(0, 20, 2)) to define breaks, and coord_cartesian(xlim = c(0, 20)) to define x range for both plots.
Revised my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.