【问题标题】:Legend title position using patchwork?图例标题位置使用拼凑?
【发布时间】:2020-11-27 22:04:51
【问题描述】:
我已经用拼凑的方式布置了 2 个图表,并在底部有一个描述它们的图例。图例具有水平方向,我试图将标题移动到顶部,而不是左侧的默认值。当我使用
guides(
fill = guide_legend(title.position = "top")
)
(连续)图例转换为离散图例。有没有简单的方法可以防止这种情况发生?
【问题讨论】:
标签:
r
ggplot2
graphics
legend
patchwork
【解决方案1】:
在ggplot 中,guide_legend 表示您需要离散的图例键。我想你正在寻找guide_colorbar。
为了演示,让我们重新创建您的问题。一、原剧情:
library(ggplot2)
set.seed(69)
df <- data.frame(x = 1:10, y = sample(10), z = 1:10)
p <- ggplot(df, aes(x, y, fill = z)) +
geom_col() +
theme(legend.position = "bottom")
p
现在,您使用的代码会导致问题:
p + guides(fill = guide_legend(title.position = "top"))
以及解决它的代码:
p + guides(fill = guide_colorbar(title.position = "top"))
由reprex package (v0.3.0) 于 2020 年 8 月 7 日创建