簡體   English   中英

從 ggplot 主題中的嵌套網格標簽中刪除面板邊框

[英]Removing panel border from nested grid labelling in ggplot theme

如何使用 ggplot 刪除網格 plot 中的上邊框和兩側邊框?

我的代碼為您提供了以下 plot。

ggplot(Plot_stomach[which(!Plot_stomach$Guild == "Piscivore"),], 
         aes(x=group, y=std_CR24, fill=group)) +
    geom_bar(stat="identity", width=0.8, color = "black") +
    geom_errorbar(aes(ymin=std_CR24-SE, ymax=std_CR24+SE), width=.2,
                  position=position_dodge(.9)) +
    geom_text(aes(x=as.numeric(group), y= std_CR24+SE+5, label = N), size = 3) +
    labs(x = "", y= expression(bold("Mean daily consmption rate (g prey kg"^-1~")"))) +
    coord_flip() +
    ylim(0,60) +
    facet_nested(.~ Guild + Cluster2 + N_name) +
    scale_fill_manual(name= "", values = c("Copepods" = "#cccccc",
                                           "Euphausiids" = "#999999", 
                                           "Larvaceans" = "#666666",
                                           "Other zooplankton" = "black",
                                           "Ammodytids" = "#d11141",
                                           "Clupeids" = "#ffc425",
                                           "Gadids" = "#00b159",
                                           "Other teleosts" = "#00aedb")) +
    theme_bw() +
    theme(panel.border = element_blank(),
          strip.text.x = element_text(colour = "black", face = "bold", size =14),
          strip.background =  element_rect(color = "black",  fill="white"),,
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.x=element_text(colour="black", size = 14, angle =45, hjust = 1),
          axis.text.y=element_text(colour="black", size = 14),
          legend.position = "none",
          text = element_text(family = "Calibri"))

這顯示了不同獵物群體的鯖魚平均消費率

但我真正想要的是

只應保留下邊界

甚至裝扮,所以我只保留主要標題的下邊框。

僅保留主標題的下邊框

要刪除條帶周圍的框,您可以設置strip.background = element_blank() 要為跨越多個較小的條帶下划線,您可以設置nest_line = element_line()

下面的簡化示例:

library(ggh4x)
#> Loading required package: ggplot2

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  facet_nested(~ vs + cyl, nest_line = element_line()) +
  theme(strip.background = element_blank())

代表 package (v2.0.1) 於 2022 年 1 月 5 日創建

編輯:

如果要為每個頂層的條帶下划線,無論它是否跨越下面的多個,都可以對頂層使用element_part_rect() ,如下所示:

library(ggh4x)
#> Loading required package: ggplot2

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  geom_point(
    data = data.frame(vs = "dummy", cyl = "dummy", mpg = 20, wt = 3)
  ) +
  facet_nested(
    ~ vs + cyl,
    strip = strip_nested(
      background_x = list(
        element_part_rect(side = "b", fill = NA, colour = "black"),
        element_rect(colour = NA, fill = NA) # repeat this line for every layer that shouldn't be underlined
      ), 
      by_layer_x = TRUE, clip = "off"
    )
  )

代表 package (v2.0.1) 於 2022 年 1 月 6 日創建

有關在此處自定義條帶主題的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM