Discontionous heatmap in R

左心房为你撑大大i 提交于 2019-12-10 21:05:28

问题


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

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.


回答1:


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)



回答2:


Generate data

d <- data.frame(x=runif(100),y=runif(100),w=runif(100))

Using ggplot2

require(ggplot2)

Sample Count

The following code produces a discontinuous heatmap where color represents the number of items falling into a bin:

ggplot(d,aes(x=x,y=y)) + stat_bin2d(bins=10)

Average weight

The following code creates a discontinuous heatmap where color represents the average value of variable w for all samples inside the current bin.

ggplot(d,aes(x=x,y=y,z=w)) + stat_summary2d(bins=10)



来源:https://stackoverflow.com/questions/28233935/discontionous-heatmap-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!