我正在制作一個帶有長圖例標簽的圖,我想調整填充鍵的大小,使其為方形(即不延伸到標簽文本的頂部和底部)。我可以使用theme()orguide_legend()使圖例鍵的高度達到我想要的高度,但我不能讓它小于包裝文本的高度。有沒有辦法覆寫這個最小尺寸?
包括最小的代碼示例——第一個成功地使密鑰變高,但第二個未能使密鑰變短(這是我想要發生的)。
theme_set(theme_bw())
df <- data.frame(x = rnorm(100))
ggplot(df)
geom_histogram(
aes(x = x, fill = 'this text is \nway too long to \nfit on one line'))
theme(legend.key.height = unit(50, 'mm'))

ggplot(df)
geom_histogram(
aes(x = x, fill = 'this text is \nway too long to \nfit on one line'))
theme(legend.key.height = unit(0.001, 'mm'))

uj5u.com熱心網友回復:
我不知道有一種方法可以在本地做到這一點。我認為您的兩個選擇是按照 Stefan 提供的鏈接中的方式撰寫自己的繪圖密鑰,或者嘗試一些更簡單但更像是 hack 的方法,如下所示:
legend_height <- 8
ggplot(df)
geom_histogram(
aes(x = x, fill = 'this text is \nway too long to \nfit on one line'),
key_glyph = draw_key_path)
guides(fill = guide_legend(override.aes = list(
size = legend_height, colour = "#f8766d")))

或者,使用legend_height <- 2,您會得到:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427618.html
