Question
GGplot: extend axis/facet lines into the panel/axis labels
Edit: I added a reprex below. My goal is to add the red lines on this faceted chart.
So I have this chart showing regression output, with variables grouped by category, I have used facet_grid
with 'free'
scales and removing panel.spacing
to get this grouped plot and I think it looks great! However, I want to expand the segment lines within the plot out to meet up with the panel. Does anyone have any ideas?
I've tried:
- playing with theme elements (nothing seems to modify that)
- annotating a line segment
- expanding the axis itself
- modifying the facet arguments
- probably a handful of other things I can't remember now.
Any advice would be much appreciated!
Edit: Reprex
library(tidyverse)
sample_df <-
tribble(~grp, ~Term, ~estimate,
'Other', 'Population Density', 2,
'Age', '15-30', 1,
'Age', '30-45', 0,
'Age', '45-65', -2,
'Age', '65+', -4,
'Sex', 'Male', 1.5,
'Sex', 'Other', -.5,
'Education', 'High School', -2,
'Education', 'College', 4,
'Education', 'Graduate', 2
)
sample_df %>%
ggplot(aes(
y = fct_rev(as_factor(Term)), x = estimate,
group = fct_rev(grp),
)) +
geom_bar(stat = 'identity', color = 'grey64') +
labs(
x = 'Coefficient Estimate',
y = NULL,
) +
facet_grid(grp~., scales='free', space='free_y', switch='y') +
scale_x_continuous(breaks = scales::breaks_width(5), minor_breaks = scales::breaks_width(1)) +
theme(
strip.placement = 'outside',
strip.clip='off',
panel.spacing = unit(0, 'in'),
panel.border = element_rect(color='gray33', fill=NA, linetype=1)
)
2 41
2