Search code examples
rheatmappheatmap

Discontionous heatmap in R


I wish to create something similar to the following types of discontinuous heat map in R:

enter image description here

enter image description here

My data is arranged as follows:

k_e percent time
 ..   ..     ..
 ..   ..     ..

I wish k_e to be x-axis, percent on y-axis and time to denote the color.

All links I could find plotted a continuous matrix http://www.r-bloggers.com/ggheat-a-ggplot2-style-heatmap-function/ or interpolated. But I wish neither of the aforementioned, I want to plot discontinuous heatmap as in the images above.


Solution

  • The second one is a hexbin plot If your (x,y) pairs are unique, you can do an x y plot, if that's what you want, you can try using base R plot functions:

    x <- runif(100)
    y<-runif(100)
    time<-runif(100)
    
    pal <- colorRampPalette(c('white','black'))
    #cut creates 10 breaks and classify all the values in the time vector in
    #one of the breaks, each of these values is then indexed by a color in the
    #pal colorRampPalette.
    
    cols <- pal(10)[as.numeric(cut(time,breaks = 10))]
    
    #plot(x,y) creates the plot, pch sets the symbol to use and col the color 
    of the points
    plot(x,y,pch=19,col = cols)
    

    With ggplot, you can also try:

    library(ggplot2)
    qplot(x,y,color=time)