简体   繁体   中英

Removing panel border from nested grid labelling in ggplot theme

How can i remove the upper and both side borders in my grid plot using ggplot?

My code gives you following 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"))

This is showing mean consumption rates of mackerels for different prey groups

But what I really want is something like

Only lower borders should be kept

or even costumize so I only keep lower border of main titles.

Only lower border of main title kept

For removing the boxes around the strips, you can set strip.background = element_blank() . For underlining the strips that span multiple smaller ones, you can set nest_line = element_line() .

Simplified example below:

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

Created on 2022-01-05 by the reprex package (v2.0.1)

EDIT:

If you want to underline the strip of every top layer, regardless of whether it spans multiple ones below, you can use element_part_rect() for the top layer as follows:

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

Created on 2022-01-06 by the reprex package (v2.0.1)

More info on customising the theming of strips here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM