带颜色阈值和网格的R矩阵图 [英] R matrix plot with colour threshold and grid

查看:112
本文介绍了带颜色阈值和网格的R矩阵图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的矩阵由0到100之间的值组成,尺寸为100 x 100. 我基本上想绘制此矩阵,但将所有高于50的值上色,例如红色及以下,例如蓝色.最重要的是,我想添加一个漂亮的Grayisch网格,就像他们在这里用ggplot一样:

my matrix consists of values between 0 and 100 and has the dimensions of 100 x 100. I basically want to plot this matrix but colour all values above 50 in e.g. red and below in e.g. blue. On top of that I'd like to add a nice grayisch grid like they do it here with ggplot:

我想知道最简单的方法是什么?我不确定是否要尝试ggplot,因为它与到目前为止所看到的相比看起来非常复杂.这样的任务还没有其他简单的绘图功能吗?

I am wondering what's the easiest way to achieve that? I am not sure if I want to give ggplot a try as it looks pretty complicated from what I have seen so far. Isn't there any other easy plot function for such a task?

推荐答案

我不确定100%是否您的数据在矩阵中,是否需要热图类型图.或者,如果采用其他形式,并且您想要散点图,例如链接到的散点图.我只是假设您的数据如前所述,并且您想要一个热图.我想这是这样的:

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)

因此,我将对数据进行整形,使其看起来像xy坐标,并带有:

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")

然后使用以下命令调用ggplot:

Then call ggplot with:

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

这篇关于带颜色阈值和网格的R矩阵图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆