在 ggplot 中更改图例标题会改变显示的图例美学

Changing legend title in ggplot changes the shown legend aesthetic

我用 ggplot2 做了一些这样的情节:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point()

一切都很好,但是我设置了另一个图例标题,如图所示here,图例中的某些点的连续颜色渐变发生变化:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point() +
  guides(col= guide_legend(title= "Some title"))

如何只更改标题而不是传说中的东方人?

此处使用的数据:

df <- data.frame(x= 1:10, y= 1:10, col= rnorm(10))

您需要 guide_colourbar 而不是 guide_legend

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  guides(col = guide_colourbar(title = "Some title"))

虽然就个人而言,我通常只是更改颜色审美的标签:

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  labs(color = "Some title")

用更少的击键得到相同的结果。