如何在ggarrange中编辑通用图例标题?
How To Edit Common Legend Title In ggarrange?
如何使用 ggarrange
编辑常见的图例标题(将其设为粗体并放大字体大小)?
根据我的六个图(p1 到 p6),我认为以下方法可行:
p6 <- p6 + theme(legend.title = element_text(size = 15, face = "bold")
下面的ggarrange
用于组合六个地块:
p <- ggarrange(p1, p2, p3, p4, p5, p6,
common.legend = TRUE,
legend = "bottom",
labels = c("1", "2", "3", "4", "5", "6"),
# font.label = list(size = 10, color = "green"),
nrow = 2, ncol = 4
)
然而,这根本没有改变普通的图例。
您可以从您感兴趣的情节中提取图例,然后将该图例与您的情节并排排列。
#libraries:
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(grid)
#example plots:
p1 <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=qsec, color = factor(cyl)))
p2 <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=4*drat, color = factor(cyl))) +
theme(legend.title = element_text(size = 15, face = "bold"),
legend.position="bottom")
#function to extract the legend of a ggplot; source:
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
get_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
#arranging the legend and plots in a grid:
p2_legend <- get_legend(p2)
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"), nrow=2),
p2_legend,
nrow=2,heights=c(10, 1))
如何使用 ggarrange
编辑常见的图例标题(将其设为粗体并放大字体大小)?
根据我的六个图(p1 到 p6),我认为以下方法可行:
p6 <- p6 + theme(legend.title = element_text(size = 15, face = "bold")
下面的ggarrange
用于组合六个地块:
p <- ggarrange(p1, p2, p3, p4, p5, p6,
common.legend = TRUE,
legend = "bottom",
labels = c("1", "2", "3", "4", "5", "6"),
# font.label = list(size = 10, color = "green"),
nrow = 2, ncol = 4
)
然而,这根本没有改变普通的图例。
您可以从您感兴趣的情节中提取图例,然后将该图例与您的情节并排排列。
#libraries:
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(grid)
#example plots:
p1 <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=qsec, color = factor(cyl)))
p2 <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=4*drat, color = factor(cyl))) +
theme(legend.title = element_text(size = 15, face = "bold"),
legend.position="bottom")
#function to extract the legend of a ggplot; source:
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
get_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
#arranging the legend and plots in a grid:
p2_legend <- get_legend(p2)
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"), nrow=2),
p2_legend,
nrow=2,heights=c(10, 1))