gpt4 book ai didi

删除由 ggarrange 排列的透明 ggplot 图之间的网格

转载 作者:行者123 更新时间:2023-12-05 05:57:16 32 4
gpt4 key购买 nike

我正在尝试生成一个图形,其中包含多个 ggplot2 图,这些图由 ggpubr 包中的 ggarrange 排列,并在左侧进行了注释。 ggplot2 图是透明的或具有透明背景。

最近出现了这个问题(即我今年早些时候没有面对它),在图和左侧的注释文本之间出现一条浅灰色线,如下图所示。并且在 LaTeX 中插入图形时更加突出。

我尝试过单独的情节,但是没有plot.border只有panel.border,因此,我的所有尝试都不成功!

我的机器信息:

Ubuntu 20.04.2 LTS 上的 R 3.6.3

ggplot2 3.3.5

ggpubr 0.4.0

11111111111

我用来制作它的代码(并受到 this 的启发):

library(ggplot2)
library(ggpubr)

# Box plot (bp)
bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")+

theme_bw(base_size = 8)+

theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size = 10),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black",fill=NA,
size=0.1, linetype="solid"),
legend.key.width = unit(5, "mm"),
legend.spacing = unit(1, 'mm'),
legend.key.size = unit(0.5, "mm"),
legend.margin = margin(3,1,3,1, unit = "mm"))+

theme(panel.grid.major =element_line(colour = "black",linetype = "dotted",size = 0.05),
panel.grid.minor = element_blank(),
plot.margin = margin( 2.25,4.5,2.25,1, unit = "mm"),
plot.title = element_text(margin = margin(1,0,1,0, unit = "mm"),size = 10),
axis.line = element_blank(),
axis.text = element_text(colour = "black"), axis.title= element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size=0.75),
panel.background = element_rect(color = NA),
rect = element_rect(fill = "transparent") )+ # all rectangles

labs(title = expression("(a)"))+
theme(axis.text = element_text(colour = "black"))


# Dot plot (dp)
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco", binwidth = 1)+

theme_bw(base_size = 8)+

theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size = 10),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black",fill=NA,
size=0.1, linetype="solid"),
legend.key.width = unit(5, "mm"),
legend.spacing = unit(1, 'mm'),
legend.key.size = unit(0.5, "mm"),
legend.margin = margin(3,1,3,1, unit = "mm"))+

theme(panel.grid.major =element_line(colour = "black",linetype = "dotted",size = 0.05),
panel.grid.minor = element_blank(),
plot.margin = margin( 2.25,4.5,2.25,1, unit = "mm"),
plot.title = element_text(margin = margin(1,0,1,0, unit = "mm"),size = 10),
axis.line = element_blank(),
axis.text = element_text(colour = "black"), axis.title= element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size=0.75),
panel.background = element_rect(color = NA),
rect = element_rect(fill = "transparent") )+ # all rectangles

labs(title = expression("(b)"))+
theme(axis.text = element_text(colour = "black"))

bxp1<-bxp+labs(title = expression("(c)"))

dp1<-dp+labs(title = expression("(d)"))

figure <- ggarrange(bxp,bxp1,dp,dp1,
ncol = 2, nrow = 2,align = c("hv"))

figure <-annotate_figure(figure,left = text_grob("left side ",
color = "black",size = 10, rot = 90))


ggsave(plot = figure,
filename = paste0("question.png"),
height = 180, width =180, units = "mm", dpi = 300, device = "png",limitsize = FALSE)

编辑:

我可以通过删除 rect = element_rect(fill = "transparent") 来忘记透明度,但是,浅灰色网格线出现在左侧的排列图和注释文本之间。

问题可能是:如何将 textgrob() 的背景改为白色而不是透明?

enter image description here

最佳答案

透明排列图的问题仍然存在。但是,渐进式解决方案可以是:

  1. 通过删除 rect = element_rect(fill = "transparent") 生成具有白色背景的 ggplot2 图。
  2. 使用另一种方法来注释和排列图:
library(gridExtra)
library(grid)
library(gridtext)

yleft <- textGrob("left side",
rot = 90, gp = gpar(fontsize = 10, fill= "white",color="black"))

figure <- grid.arrange(bxp,bxp1,
dp,dp1,
ncol = 2, nrow = 2,
left=yleft)

更新(2022 年 3 月):使用问题中的方法并将背景 bg 更改为白色。

ggsave(plot = figure,
filename = paste0("question.png"),
height = 180, width =180, units = "mm", dpi = 300,
device = "png",limitsize = FALSE,bg="white")

enter image description here

关于删除由 ggarrange 排列的透明 ggplot 图之间的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68865236/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com