facet_wrap() 中的线而不是框

Line instead of box in facet_wrap()

我正在尝试用分面清理 ggplot,这样我就可以显示一条简单的线(相当于 的底线),而不是分面标签周围的典型框。

下面的代码几乎让我一路走来,但与期望的输出不同。

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

期望的输出(手动编辑)。理想情况下,线的长度会比 panel.background 稍短(这样它就不会触及 y 轴)

也许使用注释+分段

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

不完全是你想要的,但接近...

ggplot(mtcars, aes(mpg, hp)) +
theme_bw() +
theme(
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  strip.background = element_blank(),
  panel.border = element_rect(colour = "black")
)+
geom_point() +
facet_wrap(~cyl)