移除 ggplot facet strip 标签周围的三边边框

Posted

技术标签:

【中文标题】移除 ggplot facet strip 标签周围的三边边框【英文标题】:Remove three sides of border around ggplot facet strip label 【发布时间】:2019-06-25 13:51:09 【问题描述】:

我有以下图表:

并且想做一个我认为非常简单的改变:我想删除左侧分面标签边框线的顶部、右侧和底部。

如何删除这些线条,或绘制与右手线条等效的线条?如果可能的话,我宁愿不要乱搞乱七八糟的东西,但不会拒绝任何可行的解决方案。

图形代码:

library(ggplot2)
library(dplyr)
library(forcats)

posthoc1 %>% 
  mutate(ordering = -as.numeric(Dataset) + Test.stat,
         Species2 = fct_reorder(Species2, ordering, .desc = F)) %>% 
  ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) + 
  geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) + 
  ylab("") + 
  theme_classic(base_size = 20) +
  facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y",  switch = "y") +
  geom_vline(xintercept = 0) +
  theme(axis.text.x=element_text(colour = "black"), 
        strip.placement = "outside", 
        strip.background.x=element_rect(color = NA,  fill=NA), 
        strip.background.y=element_rect(color = "black",  fill=NA)) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits=NULL)

数据:

structure(list(Dataset = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 5L, 5L, 5L, 5L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L), .Label = c("All.habitat", "Aut.habitat", "Habitat.season", 
"Lit.season", "Spr.habitat"), class = "factor"), Species = structure(c(1L, 
2L, 3L, 5L, 6L, 10L, 11L, 12L, 13L, 1L, 3L, 5L, 6L, 13L, 1L, 
2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L), .Label = c("Ar.sp1", 
"Ar.sp2", "Arc.sp1", "B.pus", "Dal.sp1.bumps", "Dip.unID", "I.palladium", 
"Pale", "Ph.sp3", "Port", "Somethus", "sty", "Sty.sp1"), class = "factor"), 
    Species2 = structure(c(2L, 9L, 1L, 4L, 5L, 7L, 11L, 12L, 
    13L, 2L, 1L, 4L, 5L, 13L, 2L, 9L, 4L, 5L, 6L, 10L, 8L, 7L, 
    11L, 13L), .Label = c("Arcitalitrus sp1", "Armadillidae sp1 ", 
    "Brachyiulus pusillus ", "Dalodesmidae sp1", "Diplopoda", 
    "Isocladosoma pallidulum ", "Ommatoiulus moreleti ", "Philosciidae sp2", 
    "Porcellionidae sp1", "Siphonotidae sp2", "Somethus sp1", 
    "Styloniscidae ", "Styloniscidae sp1"), class = "factor"), 
    Taxa = structure(c(3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L), .Label = c("Amphipoda", 
    "Diplopoda", "Isopoda"), class = "factor"), Variable = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Autumn", "Litter", 
    "Spring", "Summer"), class = "factor"), Coef = c(1.911502938, 
    2.086917154, 1.571872993, 12.61184801, 15.6161116, -1.430032837, 
    -12.51944478, 12.33934516, -8.040249562, 8.08258816, 1.780142396, 
    12.88982576, 16.78107544, -13.22641153, 1.68810887, 2.093965381, 
    12.27209197, 15.08328526, -6.334640911, -11.29985948, -11.62658947, 
    -1.676293808, -6.246555908, -3.470297147), SE = c(0.403497472, 
    2.21607562, 0.348600794, 2.423896379, 0.509468128, 3.423013791, 
    2.382857733, 1.775086895, 2.087788334, 2.23631504, 0.33402261, 
    2.518562443, 0.459720131, 1.950974996, 0.2476205, 0.235648095, 
    1.815155489, 0.325804415, 2.564680067, 2.437104984, 2.212583358, 
    2.677618401, 2.324019051, 0.420436743), Test.stat = c(18.36532749, 
    13.27324683, 13.29039037, 20.50277493, 44.06097153, 10.55234932, 
    14.64951518, 13.22575401, 20.16415411, 16.55627107, 11.81407568, 
    15.15213717, 40.67205188, 12.62233207, 37.60085488, 16.90879258, 
    20.20215107, 80.30520371, 13.35250626, 13.01692428, 17.52987519, 
    20.03658771, 12.02467914, 53.5052683)), row.names = 10:33, class = "data.frame")

【问题讨论】:

我假设您知道(因为您在代码中使用了它)构面标签边框由element_rect() 控制,它同时控制所有四个边。如果你只想要它的一侧,破解 grobs 可以工作,虽然我不确定这是否真的会改善这个图表的外观...... 是的,我知道。这就是为什么我觉得这很困难。这张图可能不是最好的例子,但在右侧有一个条形图可以更清楚地说明每组物种属于哪个组。 【参考方案1】:

此解决方案基于 grobs:找到“strip-l”(左条)的位置,然后将 rect grobs 替换为 line grobs。

p <- posthoc1 %>% 
  mutate(ordering = -as.numeric(Dataset) + Test.stat,
         Species2 = fct_reorder(Species2, ordering, .desc = F)) %>% 
  ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) + 
  geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) + 
  ylab("") + 
  theme_classic(base_size = 20) +
  facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y",  switch = "y") +
  geom_vline(xintercept = 0) +
  theme(axis.text.x=element_text(colour = "black"), 
        strip.placement = "outside", 
        #strip.background.x=element_rect(color = "white",  fill=NULL), 
        strip.background.y=element_rect(color = NA)
  ) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits=NULL)

library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"), 
                   gp=gpar(col="red", lwd=4))

for (k in grep("strip-l",q$layout$name)) 
  q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg


grid.draw(q)

【讨论】:

太好了,谢谢。我实际上想要分类单元名称下的行,所以我将代码更改为 x=unit(c(1,1),"npc"), y=unit(c(1,0),"npc") Marco Sandri 的解决方案创造了奇迹,是澄清图表的好方法。在切换轴等时,II 仍然难以完全理解 x 和 y 输入,但一些反复试验通常会有所帮助。只是想知道,在此基础上,是否可以省略一两个标签 + 行,例如假设我想显示所有这些,但 isopoda + isopoda 标签的行...?有什么想法吗? Woooow,好吧,这救了我,虽然我需要改变它来处理沿 x 轴的分组(我使用 "strip-b"(底部条带)而不是 "strip-l" 并更改了xylinesGrob() 相应)。我很确定没有其他方法可以做到这一点。太棒了,@Marco Sandri!

以上是关于移除 ggplot facet strip 标签周围的三边边框的主要内容,如果未能解决你的问题,请参考以下文章

修改 ggplot 中的 facet (strip) *variable* 标签

R语言ggplot2可视化分面图(faceting)可视化分面条形图(facet_wrap bar plot)使用strip.text函数自定义分面图每个分面标题条带strip的大小(cutomi

R语言ggplot2可视化分面图(faceting):ggplot2可视化分面图并移除分面图之间的边框线以及分面图之间的间隙(Remove Spacing between Panels)

strip.position external in 两列 facet_wrap 图

R语言ggplot2可视化为分面图facet添加通用标签实战

更改 facet wrap ggplot 中的标签 [重复]