Marking specific tiles in geom_tile() / geom_raster()

后端 未结 2 1844
醉酒成梦
醉酒成梦 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:26

    Here are two possible approaches:

    In Example 1, I used ifelse and scale_size_manual to control whether a point is plotted in each cell.

    In Example 2, I created a small auxiliary data.frame and used geom_rect to plot a rectangle instead of a dot. For convenience, I converted Var2 to factor. In ggplot2, each step along a discrete/factor axis is length 1.0. This allows easy computation of the values for geom_rect.

    # Using ggplot2 version 0.9.2.1
    library(ggplot2)
    
    # Test dataset from original post has been assigned to 'molten'.
    
    molten$Var2 = factor(molten$Var2)
    
    # Example 1.
    p1 = ggplot(data=molten, aes(x=Var1, y=Var2, fill=value)) +
         geom_raster() +
         scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
         geom_point(aes(size=ifelse(na, "dot", "no_dot"))) +
         scale_size_manual(values=c(dot=6, no_dot=NA), guide="none") +
         labs(title="Example 1")
    
    ggsave(plot=p1, filename="plot_1.png", height=3, width=3.5) 
    

    enter image description here

    # Example 2.
    # Create auxiliary data.frame.
    frames = molten[molten$na, c("Var1", "Var2")]
    frames$Var1 = as.integer(frames$Var1)
    frames$Var2 = as.integer(frames$Var2)
    
    p2 = ggplot(data=molten) +
         geom_raster(aes(x=Var1, y=Var2, fill=value)) +
         scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
         geom_rect(data=frames, size=1, fill=NA, colour="black",
           aes(xmin=Var1 - 0.5, xmax=Var1 + 0.5, ymin=Var2 - 0.5, ymax=Var2 + 0.5)) +
         labs(title="Example 2")
    
    ggsave(plot=p2, filename="plot_2.png", height=3, width=3.5) 
    

    enter image description here

    0 讨论(0)
  • 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

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