rggplot2facet-wrap

Line instead of box in facet_wrap()


I am trying to clean a ggplot with facets so that instead of the typical box around the facet label I can show a simple line (equivalent to the bottom line of the ).

The following code gets me almost all the way, but different from the desired output.

ggplot(mtcars, aes(mpg, hp)) +
 geom_point() +
 facet_wrap(~cyl) +
 theme_classic() +
 theme(strip.background = element_blank())

Desired output (edited manually). Ideally the length of the line would be slightly shorter than the panel.background (so that it doesn't touch the y axis)

enter image description here


Solution

  • Maybe using annotate + segment

    ggplot(mtcars, aes(mpg, hp)) +
        geom_point() +
        facet_grid(~cyl,) +
        theme_classic() +
        theme(strip.background = element_rect(color = "white"),
              strip.placement = "inside",
              strip.text = element_text(vjust = -1)) + 
        annotate("segment",x = 10,xend = 34,y = 375,yend = 375, size = 0.5,) + 
        coord_cartesian(ylim = c(0, 360), clip="off")
    

    enter image description here