Marking specific tiles in geom_tile() / geom_raster()

后端 未结 2 1857
醉酒成梦
醉酒成梦 2020-12-04 22:51

Let\'s say I have a data.frame like this:

df <- matrix( rnorm(100), nrow = 10)
rownames(df) <- LETTERS[1:10]
molten <- melt(df)
molten$na <- FALS         


        
2条回答
  •  生来不讨喜
    2020-12-04 23:36

    As @joran suggested in the comments, you can pass a subset of the data to a particular layer.

    Using your example data

    g <- ggplot( molten ) +
      geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
      scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
      geom_point(data = molten[molten$na,], aes( x = Var1, y = Var2, size= as.numeric(na) ) )
    
    
    g
    

    enter image description here

    If you wanted the legend to say something about what the dots signify

     g <- ggplot( molten ) +
      geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
      scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
      geom_point(data = molten[molten$na,], aes( x = Var1, y = Var2, colour = 'black' )) +
      scale_colour_manual(name = 'Ooh look', values = 'black', labels = 'Something cool')
    

    enter image description here

提交回复
热议问题