Changing legend names without changing colors in ggplot2

前端 未结 2 1019
太阳男子
太阳男子 2021-02-19 15:16

I would like to rename the values in a legend without altering the custom colors that have already been set. Is there a way to set legend labels without using scale_color_manual

相关标签:
2条回答
  • 2021-02-19 15:48

    It is possible to specify the label names in scale_colour_manual.

    ggplot(dat, aes(XValue, color = Distribution)) +
      stat_density(geom = "path", position = "identity", size = 2) +
      scale_color_manual(values = c("yellow", "black", "forestgreen"),
                         labels = c("Distribution 1",
                                    "Distribution 2",
                                    "Distribution 3"))
    

    enter image description here

    0 讨论(0)
  • 2021-02-19 15:58

    If you're willing to use a consistent color palette, then you could define this as:

        mycolors <- c("red", "blue", "black", #ee4747, #fff382, #f1f6c8, #334d65, #263825)
    

    Now, instead of

        values = c("yellow", "black", "forestgreen")
    

    use

        values = mycolors
    
    0 讨论(0)
提交回复
热议问题