Search code examples
rggplot2ggbreak

Remove outer box of geom_bar plot with broken y-axis


I made a stacked barplot in ggplot2 and removed some interval on the y-axis using the ggbreak package. It workes fine, but it added a black box around the two subplots it created. How can I get rid of this box?

I tried to set panel.border and panel.background (as well as some other options) to element.blank() as in the following example:

library("ggplot2")
library("ggbreak")

test_df <- structure(list(cv_model_name = structure(c(2L, 2L, 2L, 2L, 2L, 
                                                      2L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 
                                                      3L, 3L, 3L, 3L), levels = c("1", "2", "3", "4"), class = "factor"), 
                          best_suggestion = c(0, 1, 5, 5, 10, 10, 100, 0, 1, 5, 10, 
                                              100, 0, 5, 5, 10, 10, 100, 0, 5, 5, 10, 10, 100, 100), cv_included = c(TRUE, 
                                                                                                                     TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                     TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, 
                                                                                                                     TRUE, FALSE, TRUE, FALSE, TRUE), count = c(349, 33, 30, 1, 
                                                                                                                                                                4, 7, 1, 304, 115, 3, 24, 4, 305, 1, 28, 2, 29, 12, 760, 
                                                                                                                                                                1, 6, 28, 18, 9, 3), level = structure(c(5L, 4L, 3L, 3L, 
                                                                                                                                                                                                         2L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 3L, 3L, 2L, 2L, 1L, 5L, 
                                                                                                                                                                                                         3L, 3L, 2L, 2L, 1L, 1L), levels = c("No match", "Same genus", 
                                                                                                                                                                                                                                             "Same aggregate", "Aggregate", "Species", "Subspecies"), class = "factor"), 
                          no_cv_model = c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, 
                                          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, 
                                          FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE
                          )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
                          ), row.names = c(NA, -25L), groups = structure(list(cv_model_name = structure(c(1L, 
                                                                                                          1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
                                                                                                          4L), levels = c("1", "2", "3", "4"), class = "factor"), best_suggestion = c(0, 
                                                                                                                                                                                      1, 5, 10, 100, 0, 1, 5, 10, 100, 0, 5, 10, 100, 0, 5, 10, 100
                                                                                                          ), .rows = structure(list(8L, 9L, 10L, 11L, 12L, 1L, 2L, 3:4, 
                                                                                                                                    5:6, 7L, 19L, 20:21, 22:23, 24:25, 13L, 14:15, 16:17, 18L), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                              "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                              ), row.names = c(NA, -18L), .drop = TRUE))

safe_colorblind_palette <- c(
  "#88CCEE", "#CC6677", "#DDCC77", "#117733", "#332288", "#AA4499", "#44AA99",
  "#999933", "#882255", "#661100", "#6699CC", "#888888"
  )

options(ggplot2.discrete.colour = safe_colorblind_palette,
        ggplot2.discrete.fill = safe_colorblind_palette
        )

theme_set(theme_bw())

gg <- ggplot(
  data = test_df,
  aes(
    x = cv_model_name,
    y = count,
    fill = level,
    alpha = as.factor(cv_included)
  ),
  pattern = "stripe", pattern_angle = 0
) +
  geom_bar(position = "stack", stat = "identity") +
  xlab("Factor") +
  ylab("Count") +
  scale_fill_manual("Best match", values = safe_colorblind_palette) +
  scale_alpha_manual("CV model included", values = c(0.5, 1)) +
  scale_y_break(c(100, 300)) +
  theme(strip.background = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())
gg

However, this did only remove the inner panel borders (which I intended to keep!) while the outer box is still intact. result of my attempts


Solution

  • One option to fix that would be to set the base theme in your ggplot2 pipeline instead of via theme_set():

    library(ggplot2)
    library(ggbreak)
    
    gg <- ggplot(
      data = test_df,
      aes(
        x = cv_model_name,
        y = count,
        fill = level,
        alpha = as.factor(cv_included)
      ),
      pattern = "stripe", pattern_angle = 0
    ) +
      geom_bar(position = "stack", stat = "identity") +
      xlab("Factor") +
      ylab("Count") +
      scale_fill_manual("Best match", values = safe_colorblind_palette) +
      scale_alpha_manual("CV model included", values = c(0.5, 1)) +
      scale_y_break(c(100, 300))  +
      theme_bw() +
      theme(
        strip.background = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()
      )
      
    gg
    

    enter image description here