R matrix plot with colour threshold and grid

拟墨画扇 提交于 2019-12-05 14:19:31

I am not 100% sure if your data is in a matrix and you want a heatmap type plot. Or if it is in some other form and you want a scatterplot like those you link to. I just assumed your data is as described and that you want a heatmap. I imagine it is something like:

   x=abs(rnorm(100*100,50,25))
    x=matrix(x,nrow=100)

So I would reshape the data so it looks like xy coordinates with:

require(reshape2)
require(ggplot2)
x1=melt(x)
names(x1)=c("x","y","color")

Then I would make my cutoff into a factor:

x1$color=factor(x1$color>50)
levels(x1$color)=c("lessthan50","more than 50")

Then call ggplot with:

qplot(x, y, fill=color, data=x1,geom='tile')

In base graphics it is just:

image(x, col=c("red","blue")[1+(x>50)] )

To add the grid use:

grid(nx=100, ny=100, lty=1)

You can do it simply enough with levelplot,

x <- abs(runif(100*100,0, 100))
x <- matrix(x,nrow=100)
levelplot(x, cuts=1, col.regions=c("red", "blue"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!