plot a heatmap with a third dimension

后端 未结 2 576
心在旅途
心在旅途 2021-02-14 18:23

I would like to plot a heatmap like this

\"enter

I know how to do a normal heatmap

2条回答
  •  我寻月下人不归
    2021-02-14 18:41

    3D barchart might be a way to go. There's panel.3dbars() in the package latticeExtra that you might want to test. See the function's help page for more examples, but here's one example modified from one of the examples on the help page:

    library(latticeExtra)
    # A function generating colors
    cols<-function(n) {
       colorRampPalette(c("#FFC0CB", "#CC0000"))(20)                                 # 20 distinct colors
    }
    # The plot
    cloud(VADeaths, panel.3d.cloud = panel.3dbars, col="white",                      # white borders for bars
      xbase = 1, ybase = 1, zlim = c(0, max(VADeaths)),                              # No space around the bars
      scales = list(arrows = FALSE, just = "right"), xlab = NULL, ylab = NULL,
      col.facet = level.colors(VADeaths, at = do.breaks(range(VADeaths), 20),        
                               col.regions = cols,                                   # color ramp for filling the bars
                               colors = TRUE),
      colorkey = list(col = cols, at = do.breaks(range(VADeaths), 20)),
      screen = list(z = 65, x = -65))                                                # Adjust tilting
    

    The resulting is similar to:

    enter image description here

    Note that the data to be plotted needs to be turned into a matrix for this to work. If you have measurement from X*Y grid, where Z is the intensity of the measurement, this should be rather straightforward to pull off. The functions here (e.g., level.colors()) automatically decides the color according to the data range, but you can also generate the colors yourself, before plotting.

提交回复
热议问题