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

Lul*_*ulY 4 r legend ggplot2

我用 ggplot2 画了一些图,如下所示:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point()
Run Code Online (Sandbox Code Playgroud)

好的

一切都很好,但是一旦我设置了另一个图例标题(如此处所示)连续颜色渐变就会更改为图例中的某些点:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point() +
  guides(col= guide_legend(title= "Some title"))
Run Code Online (Sandbox Code Playgroud)

为什么

我怎样才能只改变标题而不改变传说中的东方?

这里使用的数据:

df <- data.frame(x= 1:10, y= 1:10, col= rnorm(10))
Run Code Online (Sandbox Code Playgroud)

All*_*ron 9

你需要guide_colourbar而不是guide_legend

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  guides(col = guide_colourbar(title = "Some title"))
Run Code Online (Sandbox Code Playgroud)

虽然就我个人而言,我通常只会更改色彩美学的标签:

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  labs(color = "Some title")
Run Code Online (Sandbox Code Playgroud)

这样可以用更少的击键次数获得相同的结果。