How to combine scales for colour and size into one legend?

后端 未结 2 855
广开言路
广开言路 2020-12-01 21:39

I would like to know how to colorize the size_scale in scale_size() {ggplot2} in a plot where the size and color are from the same data.

Example:

2条回答
  •  独厮守ぢ
    2020-12-01 21:42

    Use the guides() function of ggplot2. In this case:

    ggplot(df,aes(V1,V2))+
      geom_point(aes(colour=V3,size=V3))+
      scale_colour_gradient(low="grey", high="black")+
      scale_size(range=c(1,10)) +
      guides(color=guide_legend(), size = guide_legend())
    

    ggplot2 will try to integrate the scales for you. It doesn't in this case because the default guide for a color scale is a colorbar and the default guide for size is a normal legend. Once you set them both to be legends, ggplot 2 takes over and combines them.

提交回复
热议问题