删除 y 轴损坏的 geom_bar 图的外框

问题描述 投票:0回答:1

我在

ggplot2
中制作了一个堆叠条形图,并使用
ggbreak
包删除了 y 轴上的一些间隔。它工作正常,但它在它创建的两个子图周围添加了一个黑框。我怎样才能摆脱这个盒子?

我尝试将

panel.border
panel.background
(以及其他一些选项)设置为
element.blank()
,如下例所示:

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

但是,这只是删除了内面板边框(我打算保留!),而外盒仍然完好无损。

r ggplot2 ggbreak
1个回答
0
投票

解决此问题的一个选项是在

ggplot2
代码中设置基本主题,而不是通过
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

© www.soinside.com 2019 - 2024. All rights reserved.