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)
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"))
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.
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"))