【问题标题】: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 日创建

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2021-11-02
    • 1970-01-01
    • 2021-08-19
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多