删除由 ggarrange 排列的透明 ggplot 图之间的网格
Removing grid between transparent ggplot plots arranged by ggarrange
我正在尝试生成一个包含多个 ggplot2
图的图,这些图由包 ggpubr
中的 ggarrange
排列,并在左侧进行了注释。 ggplot2
图是透明的或具有透明背景。
最近出现的问题(即我今年早些时候没有面对它)如下图所示,左侧的图和注释文本之间出现一条浅灰色线。并且在LaTeX中插入图形更加突出
我试过单独的剧情,但是没有plot.border
只有panel.border
,所以,我试的都没有成功!
我的机器信息:
R 3.6.3 on Ubuntu 20.04.2 LTS
ggplot2 3.3.5
ggpubr 0.4.0
我用来制作它的代码(灵感来自 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()
的背景改为透明的白色?
透明排列图的问题仍然存在。但是,渐进式解决方案可以是:
- 通过删除
rect = element_rect(fill = "transparent")
生成具有白色背景的 ggplot2 图。
- 使用另一种方法来注释和排列图:
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")
我正在尝试生成一个包含多个 ggplot2
图的图,这些图由包 ggpubr
中的 ggarrange
排列,并在左侧进行了注释。 ggplot2
图是透明的或具有透明背景。
最近出现的问题(即我今年早些时候没有面对它)如下图所示,左侧的图和注释文本之间出现一条浅灰色线。并且在LaTeX中插入图形更加突出
我试过单独的剧情,但是没有plot.border
只有panel.border
,所以,我试的都没有成功!
我的机器信息:
R 3.6.3 on Ubuntu 20.04.2 LTS
ggplot2 3.3.5
ggpubr 0.4.0
我用来制作它的代码(灵感来自 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()
的背景改为透明的白色?
透明排列图的问题仍然存在。但是,渐进式解决方案可以是:
- 通过删除
rect = element_rect(fill = "transparent")
生成具有白色背景的 ggplot2 图。 - 使用另一种方法来注释和排列图:
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")