Plotting discrete and continuous scales in same ggplot

前端 未结 3 1156
一整个雨季
一整个雨季 2021-02-19 22:59

I would like to plot some different data items using ggplot2, using two different colour scales (one continuous and one discrete from two different df\'s). I can plot either exa

相关标签:
3条回答
  • 2021-02-19 23:10

    The problem isn't so complicated as you might think. In general, you can only map an aesthetic once. So calling scale_colour_* twice makes no sense to ggplot2. It will try to force one into the other.

    You can't have multiple colour scales in the same graph, regardless of whether either one is continuous or discrete. The package author has said that they have no intention of adding this, either. It is rather complicated to implement and would make it too easy to make incredibly confusing graphs. (Multiple y axes will never be implemented for similar reasons.)

    0 讨论(0)
  • 2021-02-19 23:11

    I don't have the time at the moment to provide a complete working example, but there's another way to do this that deserves to be mentioned here: Fill and border colour in geom_point (scale_colour_manual) in ggplot

    Basically, using geom_point in conjunction with shape = 21, color = NA allows you to control the color of a series of points using the fill rather than color aesthetic. Here's what my code looked like for this. I understand that there's no data provided, but hopefully it provides you with a starting point:

    biloxi + 
      geom_point(data = filter(train, primary != 'na'), 
                 aes(y = GEO_LATITUDE, x = GEO_LONGITUDE, fill = primary), 
                 shape = 21, color = NA, size = 1) + 
        scale_fill_manual(values = c('dodgerblue', 'firebrick')) + 
      geom_point(data = test_map_frame, 
                 aes(y = GEO_LATITUDE, x = GEO_LONGITUDE, color = var_score), 
                alpha = 1, size = 1) + 
        scale_color_gradient2(low = 'dodgerblue4', high = 'firebrick4', mid = 'white',
                        midpoint = mean(test_map_frame$var_score))   
    

    Notice how each call to geom_point invokes a different aesthetic (color or fill)

    0 讨论(0)
  • 2021-02-19 23:34

    You can do this. You need to tell grid graphics to overlay one plot on top of the other. You have to get margins and spacing etc, exactly right, and you have to think about the transparency of the top layers. In short... it's not worth it. As well as possibly making the plot confusing.

    However, I thought some people might like a pointer on how to acheive this. N.B. I used code from this gist to make the elements in the top plot transparent so they don't opaque the elements below:

    grid.newpage()
    pushViewport( viewport( layout = grid.layout( 1 , 1 , widths = unit( 1 , "npc" ) ) ) ) 
    print( p1 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1 , layout.pos.col = 1 ) )
    print( p2 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1 , layout.pos.col = 1 ) )
    

    See my answer here for how to add legends into another position on the grid layout.

    enter image description here

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