手动创建ggplot图例

时间:2020-04-21 17:43:30

标签: r ggplot2

我想为ggplot制作一个完全定制的图例。出于MWE之外的复杂原因,我不想在各种color参数中指定aes

这是我要工作的一个示例:

p = ggplot() + 
  # First plot some data from one set
  geom_point(aes(x = rnorm(10), y = rnorm(10)), col = "blue") +
  # Then plot some data from another set
  geom_point(aes(x = rnorm(10) + 1, y = rnorm(10) + 1), col = "orange") + 
  # Just to make the plot mildly cleaner
  labs(x = "X", y = "Y") +
  # Now want to manually come up with my own legend
  # However ggplot seems to ignore this
  scale_colour_manual(name = "Group", 
                    values = c("Group 1" = "blue", "Group 2" = "orange"))
# Printing the plot
p

但是,如下面所示,结果图将忽略我的scale_colour_manual调用:

enter image description here

如何独立强制自定义图例?

0 个答案:

没有答案