How to merge colour and shape?

前端 未结 3 1763
我寻月下人不归
我寻月下人不归 2021-02-01 23:38

My troubles started when I had a variable with more than 6 values because that is the current maximum value for the scale_shape function in ggplot2.

Due to that problem

相关标签:
3条回答
  • 2021-02-02 00:18

    Further trick: If you give either legend a name, you must give them both the same name. If you give only one legend a name, ggplot will separate the legends again. Amending kohske's example:

    plot1 <- ggplot(dataf, aes(x=Density, y=Growth, colour=Municipality,
            shape=Municipality)) + geom_point(size=3)
    
    plot2 <- plot1 + scale_colour_discrete() + 
    scale_shape_manual(values=as.numeric(dataf$Municipality))
    
    plot2
    
    plot3 <- plot1 + scale_colour_discrete('City') + 
    scale_shape_manual(values=as.numeric(dataf$Municipality))
    
    plot3
    
    plot4 <- plot1 + scale_colour_discrete('City') + 
    scale_shape_manual('City',values=as.numeric(dataf$Municipality))
    
    plot4
    
    0 讨论(0)
  • 2021-02-02 00:26

    What about using scale_shape_manual()? If I understood your question correctly, you don't really need to differentiate by both colour and shape and would prefer shape, right?

    ggplot(dataf, aes(x=Density, y=Growth)) + 
      geom_point(aes(shape = Municipality)) +
      scale_shape_manual(values = 1:11)
    

    produces: enter image description here

    0 讨论(0)
  • 2021-02-02 00:41

    here is an example:

    plot1 <- ggplot(dataf, aes(x=Density, y=Growth, colour=Municipality,
            shape=Municipality))
    plot1 <- plot1 + geom_point(size=3)
    plot1 <- plot1 + scale_colour_discrete() + 
    scale_shape_manual(values=as.numeric(dataf$Shape))
    plot1
    

    if you need filled shapes, then replace with

    scale_shape_manual(values=c(16, 17, 15, 3, 7, 8)[as.numeric(dataf$Shape)])
    

    the tricks are:

    1. use same variable for colour and shape aes (Municipality)
    2. use scale_shape_manual and make mapping of breaks (here, Municipality) and value (here, dataf$Shape)
    3. you need numeric variable instead of factor for values of scale_shape_manual
    0 讨论(0)
提交回复
热议问题