如何将图例框与 ggplot2 中图例标题的中间对齐?
How to align the legend box to the middle of legend title in ggplot2?
默认情况下,图例 box/key 与图例标题的左侧对齐。因为我有很长的图例标题,所以我想将它对齐到中心。我试过 theme
和 guide_legend
都没有成功。任何建议将不胜感激。谢谢!
数据
df <- structure(list(Flow = c(0.992762, 0.802408, 0.9826, 0.754863,
0.174542, 0.056777), Coef = c(0.62, 0.49, 0.38, 0.59, 0.25, 0.67
), VeryLongLegendTitle = c(4.47680710313542, 18.8500193246859,
5.82742564783431, 23.3217237977105, 13.0155332302148, 88.4960885143824
)), class = "data.frame", row.names = c(NA, -6L), .Names = c("Flow",
"Coef", "VeryLongLegendTitle"))
代码
library(ggplot2)
p1 <- ggplot(df, aes(x = Flow, y = Coef, color = VeryLongLegendTitle)) +
xlab(NULL) + scale_x_continuous(limits = c(0.0, 1.0), breaks = c(0.25, 0.75)) +
geom_point(size = 2, alpha = 0.8) +
theme_bw(base_size = 14) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5))
p1
p1 + theme(legend.title.align = 0.5)
p1 + theme(legend.title = element_text(hjust = 0.5))
p1 + guides(color = guide_legend(title.hjust = 0.5))
情节
我同意@www 但你可以试试 p1 + guides(color = guide_legend(keywidth = 5, keyheight = 2))
。它会产生这样的东西:
看看 Legend guide 也不错。
从这里借用解决方案
library(ggplot2)
library(gtable)
library(grid)
long1 <- ggplot(df, aes(x = Flow, y = Coef, color = VeryLongLegendTitle)) +
xlab(NULL) + scale_x_continuous(limits = c(0.0, 1.0), breaks = c(0.25, 0.75)) +
geom_point(size = 2, alpha = 0.8) +
theme_bw(base_size = 14) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5))
# extract legend
g <- ggplotGrob(long1)
grobs <- g$grobs
legend_index <- which(sapply(grobs, function(x) x$name) == "guide-box")
legend <- grobs[[legend_index]]
# extract guides table
guides_index <- which(sapply(legend$grobs, function(x) x$name) == "layout")
guides <- legend$grobs[[guides_index]]
# add extra column for spacing
# guides$width[5] is the extra spacing from the end of the legend text
# to the end of the legend title. If we instead distribute it 50:50 on
# both sides, we get a centered legend
guides <- gtable_add_cols(guides, 0.5*guides$width[5], 1)
guides$widths[6] <- guides$widths[2]
title_index <- guides$layout$name == "title"
guides$layout$l[title_index] <- 2
# reconstruct legend and write back
legend$grobs[[guides_index]] <- guides
g$grobs[[legend_index]] <- legend
grid.newpage()
grid.draw(g)
由 reprex package (v0.2.0) 创建于 2018-03-18。
默认情况下,图例 box/key 与图例标题的左侧对齐。因为我有很长的图例标题,所以我想将它对齐到中心。我试过 theme
和 guide_legend
都没有成功。任何建议将不胜感激。谢谢!
数据
df <- structure(list(Flow = c(0.992762, 0.802408, 0.9826, 0.754863, 0.174542, 0.056777), Coef = c(0.62, 0.49, 0.38, 0.59, 0.25, 0.67 ), VeryLongLegendTitle = c(4.47680710313542, 18.8500193246859, 5.82742564783431, 23.3217237977105, 13.0155332302148, 88.4960885143824 )), class = "data.frame", row.names = c(NA, -6L), .Names = c("Flow", "Coef", "VeryLongLegendTitle"))
代码
library(ggplot2) p1 <- ggplot(df, aes(x = Flow, y = Coef, color = VeryLongLegendTitle)) + xlab(NULL) + scale_x_continuous(limits = c(0.0, 1.0), breaks = c(0.25, 0.75)) + geom_point(size = 2, alpha = 0.8) + theme_bw(base_size = 14) + theme(axis.text.x = element_text(angle = 0, vjust = 0.5)) p1 p1 + theme(legend.title.align = 0.5) p1 + theme(legend.title = element_text(hjust = 0.5)) p1 + guides(color = guide_legend(title.hjust = 0.5))
情节
我同意@www 但你可以试试 p1 + guides(color = guide_legend(keywidth = 5, keyheight = 2))
。它会产生这样的东西:
看看 Legend guide 也不错。
从这里借用解决方案
library(ggplot2)
library(gtable)
library(grid)
long1 <- ggplot(df, aes(x = Flow, y = Coef, color = VeryLongLegendTitle)) +
xlab(NULL) + scale_x_continuous(limits = c(0.0, 1.0), breaks = c(0.25, 0.75)) +
geom_point(size = 2, alpha = 0.8) +
theme_bw(base_size = 14) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5))
# extract legend
g <- ggplotGrob(long1)
grobs <- g$grobs
legend_index <- which(sapply(grobs, function(x) x$name) == "guide-box")
legend <- grobs[[legend_index]]
# extract guides table
guides_index <- which(sapply(legend$grobs, function(x) x$name) == "layout")
guides <- legend$grobs[[guides_index]]
# add extra column for spacing
# guides$width[5] is the extra spacing from the end of the legend text
# to the end of the legend title. If we instead distribute it 50:50 on
# both sides, we get a centered legend
guides <- gtable_add_cols(guides, 0.5*guides$width[5], 1)
guides$widths[6] <- guides$widths[2]
title_index <- guides$layout$name == "title"
guides$layout$l[title_index] <- 2
# reconstruct legend and write back
legend$grobs[[guides_index]] <- guides
g$grobs[[legend_index]] <- legend
grid.newpage()
grid.draw(g)
由 reprex package (v0.2.0) 创建于 2018-03-18。