如何在 ggplot2 中重命名(组合)图例?

cro*_*oss 5 r ggplot2

是否有可能重新命名一个融合了不同美学的传奇?ggplot2

例如:

  1. 将颜色与线型结合起来:控制 ggplot 图例中的线条颜色和线型
  2. 将颜色与形状结合起来:如何使用线条符号在 colorbrewer 调色板中回收颜色

怎样才能重新命名这样一个组合的传奇呢?当我尝试使用nameforscale_shape_manualscale_color_manual组合图例时,它会分成两个不同的图例。那不是我想要的。


可能的解决方案:我在这里找到的解决方案: https: //stackoverflow.com/a/14622513/4317408对我有用......

基本上有两种可能的方法,在这两种方法中,为了使组合图例获得新名称,name组合的两个不同图例的必须修改为相同的:

  scale_color_manual(values = c(brewer.pal(5, "Set1") ) , name = "Combined Legend Title") +
  scale_shape_manual(values = c(1,2,3,4,5) , name = "Combined Legend Title")
Run Code Online (Sandbox Code Playgroud)

或者

  guides(shape=guide_legend(title="Combined Legend Title"), color=guide_legend(title="Combined Legend Title"))
Run Code Online (Sandbox Code Playgroud)

我仍然想知道是否有更简单的方法可以做到这一点?