如何去除绘图周围的边框?

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

我有一个由两个图组合而成的图。它是数据面板中的数据图(在本例中为箱线图)和治疗信息。它与我当前的主题配合得很好,但我现在想更改背景颜色,从而导致分隔两者的图周围出现边框。

这是我当前的代码:

library(patchwork)
library(ggplot2)
library(ggthemes)
library(tidyr)
library(forcats)
library(dplyr, warn = FALSE)

line_size <- 1.2 
base_size <- 20 
axis_text_rel_size = -1
title_text_rel_size = 5


d <- tibble(
  Rodent= c(rep("Mouse",11),
            rep("Hamster",7),
            rep("Guinea pig",4),
            rep("Gerbil",12)),
  `Weight (gm)` = rnorm(34,25,10),
  `Heat:` = c(rep("+",11),rep("-",7),rep("+",4),rep("+",12))
  ,  `Needle:` = c(rep("+",11),rep("+",7),rep("+",4),rep("+",12)),
  `Comb.:` = c(rep("Phen",11),rep("-",7),rep("-",4),rep("Met",12)),
)

set.seed(123)

p1 <- d %>%
  ggplot(aes(Rodent, `Weight (gm)`)) +
  geom_boxplot(lwd = 1.3, colour = "black") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  labs(y = "Percentage") +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    plot.title = element_text(
      face = "bold",
      size = rel(1), hjust = 0.5
    ),
    axis.line = element_line(colour = "black", size = line_size),
    axis.ticks = element_line(colour = "black", size = line_size),
    axis.title.x = element_blank(),
    legend.title = element_blank(),
    legend.position = "none"
  )

d1 <- d %>%
  select(-`Weight (gm)`) %>%
  pivot_longer(-Rodent) |>
  mutate(
    name = factor(name, rev(unique(name)))
  )

p2 <- d1 %>%
  ggplot(aes(Rodent, name, label = value)) +
  geom_text(size = 20 / .pt) +
  labs(x = NULL, y = NULL) +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    axis.ticks = element_blank()
  )


(p1 / p2) +
  plot_layout(heights = c(2, 1)) &
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    text = element_text(size = 20, colour = "black"),
    axis.title = element_text(face = "bold", size = rel(1)),
    axis.title.y = element_text(angle = 90, vjust = -8),
    axis.text = element_text(face = "bold", size = rel(1)),
    axis.text.x = element_blank(),
    plot.background = element_rect(fill = "#f0f0f0"),
    plot.margin = margin(5.5, 5.5, 5.5, -5.5)
  )

这给了我这个数字:

enter image description here

我正在使用

plot.background = element_rect(fill = "#f0f0f0")
来更改背景,但这会引入我不想要的边框/网格线。

有没有办法改变背景颜色而不出现黑线?

r ggplot2
1个回答
0
投票

我正在使用:

plot.background = element_rect(color  = 'black', 
                                     size = 1,  
                                     linetype = 'solid', 
                                     fill = c_plot_background)
     )

c_plot_background 是一个定义的颜色,如“黑色”, 你可以将“大小”设置为 0 (这是我在这里的第一篇文章...)

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