I am trying to expand the geom_vline lines in my ggplot plot to go outside of the plot space and into the axis area. The goal of this is to have these lines separating the axis labels so it can line up with another plot that goes next to it (see below).
Some short example code (I have many more rows, and therefore need the horizontal lines to keep things straight):
library(ggplot2)
library(cowplot)
library(dplyr)
#play data set
cars.data <- mtcars %>%
mutate(car_name = rownames(mtcars)) %>%
slice(1:6)
#I would like vlines to be extend in this plot
p1 <- ggplot(cars.data, aes(x = car_name, y = hp)) +
geom_point() +
scale_x_discrete(position = "top") +
coord_flip() +
geom_vline(aes(xintercept = seq(1.5, 6.5, 1)), color = "gray60") +
xlab("")
p2 <- ggplot(cars.data, aes(y = car_name, x = 1)) +
geom_text(aes(label = disp)) +
xlab("disp") +
geom_hline(aes(yintercept = seq(1.5, 6.5, 1)), color = "gray60")+
theme(axis.title.y = element_blank(),
axis.title.x = element_text(vjust = 0.5, angle = 30),
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "gray90"))
plot_grid(p1, p2, rel_widths = c(1,0.2))
This results in the following figure:
What I am looking for is to extend the lines from p1
so that they continue between the plots, almost like a plot-table hybrid. I've tried clip = "off"
but it doesn't seem to do the trick.