0

我已经使用拼凑设计了 2 个图表,并在底部有一个描述它们的图例。图例具有水平方向,我试图将标题移动到顶部,而不是左侧的默认值。当我使用

guides(
fill = guide_legend(title.position = "top")
) 

(连续)图例转换为离散图例。有没有一种简单的方法可以防止这种情况发生?

4

1 回答 1

4

ggplot中,aguide_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 包(v0.3.0)于 2020-08-07 创建

于 2020-08-07T15:02:58.150 回答