如何在 ggplot2 中为单个 geom/color 组合创建图例条目?

How do you create a legend entry for a single geom/color combination in ggplot2?

我想在 ggplot2 中为只有一个值的 geom/color 组合添加一个图例。例如:

library(tidyverse)
lala <- tibble(
  haha = runif(100),
  baba = runif(100) - 2 , 
  equis = 1:100,
  cow = factor(round(runif(100)))
)
  runner <- tibble(ecks = rnorm(100) * 100, why = rnorm(100))

  ggplot(data = lala ) + 
geom_ribbon(mapping = aes(x = equis, ymax = haha, ymin = baba, fill = cow)) + 
geom_point(data = runner, aes( x = ecks, y = why))

丝带有图例,但我想要第二个图例,其中只有一个黑点条目。我知道这是一个愚蠢的例子,但我正在研究一个更严肃的例子。有没有办法在没有标题或注释的情况下做到这一点?谢谢。

一种方法是使用 geom_point() 美学中的形状参数。然后如果你需要重命名它,你可以使用 labs() 函数。

ggplot(data = lala ) +
  geom_ribbon(mapping = aes(x = equis, ymax = haha,ymin = baba, fill = cow)) +
  geom_point(data = runner, aes(x = ecks, y = why, shape = "")) +
  labs(shape="name goes here")