4 回答

TA貢獻1824條經(jīng)驗 獲得超8個贊
ggplot2 v3.0.0于2018年7月發(fā)布的版本具有修改的工作選項legend.spacing.x,legend.spacing.y并且legend.text。
示例:增加圖例鍵之間的水平間距
library(ggplot2)
ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
geom_bar() +
coord_flip() +
scale_fill_brewer("Cyl", palette = "Dark2") +
theme_minimal(base_size = 14) +
theme(legend.position = 'top',
legend.spacing.x = unit(1.0, 'cm'))
注意:如果只想在圖例文本的右側(cè)擴展間距,請使用 stringr::str_pad()
示例:將圖例鍵標(biāo)簽移至底部并增加垂直間距
ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
geom_bar() +
coord_flip() +
scale_fill_brewer("Cyl", palette = "Dark2") +
theme_minimal(base_size = 14) +
theme(legend.position = 'top',
legend.spacing.x = unit(1.0, 'cm'),
legend.text = element_text(margin = margin(t = 10))) +
guides(fill = guide_legend(title = "Cyl",
label.position = "bottom",
title.position = "left", title.vjust = 1))
示例:用于scale_fill_xxx&guide_colorbar
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(fill = hp), pch = I(21), size = 5)+
scale_fill_viridis_c(guide = FALSE) +
theme_classic(base_size = 14) +
theme(legend.position = 'top',
legend.spacing.x = unit(0.5, 'cm'),
legend.text = element_text(margin = margin(t = 10))) +
guides(fill = guide_colorbar(title = "HP",
label.position = "bottom",
title.position = "left", title.vjust = 1,
# draw border around the legend
frame.colour = "black",
barwidth = 15,
barheight = 1.5))
對于垂直圖例,設(shè)置legend.key.size只會增加圖例鍵的大小,而不會增加它們之間的垂直間隔
ggplot(mtcars) +
aes(x = cyl, fill = factor(cyl)) +
geom_bar() +
scale_fill_brewer("Cyl", palette = "Dark2") +
theme_minimal(base_size = 14) +
theme(legend.key.size = unit(1, "cm"))
為了增加圖例鍵之間的距離,需要修改legend-draw.r功能。有關(guān)更多信息,請參見此問題
# function to increase vertical spacing between legend keys
# @clauswilke
draw_key_polygon3 <- function(data, params, size) {
lwd <- min(data$size, min(size) / 4)
grid::rectGrob(
width = grid::unit(0.6, "npc"),
height = grid::unit(0.6, "npc"),
gp = grid::gpar(
col = data$colour,
fill = alpha(data$fill, data$alpha),
lty = data$linetype,
lwd = lwd * .pt,
linejoin = "mitre"
))
}
# register new key drawing function,
# the effect is global & persistent throughout the R session
GeomBar$draw_key = draw_key_polygon3
ggplot(mtcars) +
aes(x = cyl, fill = factor(cyl)) +
geom_bar() +
scale_fill_brewer("Cyl", palette = "Dark2") +
theme_minimal(base_size = 14) +
theme(legend.key = element_rect(color = NA, fill = NA),
legend.key.size = unit(1.5, "cm")) +
theme(legend.title.align = 0.5)

TA貢獻1831條經(jīng)驗 獲得超9個贊
我認(rèn)為最好的選擇是guide_legend在內(nèi)使用guides:
p + guides(fill=guide_legend(
keywidth=0.1,
keyheight=0.1,
default.unit="inch")
)
注意使用default.unit,無需加載grid程序包。

TA貢獻1845條經(jīng)驗 獲得超8個贊
我用于在水平圖例中添加空間的簡單解決方案,只需在標(biāo)簽中添加空間(請參見下面的摘錄):
scale_fill_manual(values=c("red","blue","white"),
labels=c("Label of category 1 ",
"Label of category 2 ",
"Label of category 3"))

TA貢獻1840條經(jīng)驗 獲得超5個贊
要在圖例中的條目之間增加間距,請調(diào)整主題元素的邊距l(xiāng)egend.text。
要在每個圖例標(biāo)簽的右側(cè)添加30pt的空間(對于水平圖例可能有用):
p + theme(legend.text = element_text(
margin = margin(r = 30, unit = "pt")))
在每個圖例標(biāo)簽的左側(cè)添加30pt的空間(對于垂直圖例可能有用):
p + theme(legend.text = element_text(
margin = margin(l = 30, unit = "pt")))
對于一個ggplot2對象p。關(guān)鍵字是legend.text和margin。
[有關(guān)編輯的注意事項:首次發(fā)布此答案時,存在一個錯誤。該錯誤現(xiàn)已修復(fù)]
- 4 回答
- 0 關(guān)注
- 7626 瀏覽
添加回答
舉報