ggplot wrong color assignment

后端 未结 1 1985
别那么骄傲
别那么骄傲 2021-01-05 10:35

I implemented the following function to plot the different colors used by ggplot. They function also plots the hex color values in the legend. However, somehow, the assignme

相关标签:
1条回答
  • 2021-01-05 10:59

    As you want to supply color names to argument colour= and display also a legend for this argument, you should add scale_colour_identity() to your last line in function. This scale ensures that values supplied will be interpreted as actual color values. Adding of argument breaks=cols_hex in function scale() will ensure ordering of names in legend.

    ggplot(NULL) +     
    geom_point(data=data, aes(x=x, y=y, colour=cols_hex), size=size, alpha=alpha) + 
            scale_colour_identity(guide="legend",breaks=cols_hex)
    

    enter image description here

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