I would like to plot a geom underneath the x-axis: a set of bars to highlight interesting intervals of x values.
I've hacked my way to a solution by turning off the axes altogether and drawing the geoms inside the plot. The issue is that now I have to add axes elements (line, ticks, labels) one by one and by hand.
It would be simpler to draw the bars in the margins of the plot instead. Is it possible to do with ggplot2
?
library("tidyverse")
data <-
tribble(
~x_start, ~x_end, ~y, ~label,
1, 5, 9, "A",
3, 6, 8, "B",
8, 11, 5, "C",
9, 12, 6, "D"
)
data %>%
ggplot()
# Add fake x-axis.
geom_hline(
yintercept = 0
)
# Add segments "inside the plotting area".
geom_segment(
aes(
x = x_start,
y = y,
xend = x_end,
yend = y,
color = label
),
size = 2
)
# Add tiles "inside the margins".
geom_rect(
aes(
xmin = x_start,
ymin = 0,
xmax = x_end,
ymax = -1,
fill = label
),
alpha = 0.5
)
theme_minimal()
theme(
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.position = "none"
)