简体   繁体   English

R绘制数字矩阵的2D曲面

[英]R plot 2D surface of a matrix of numbers

I am currently trying, given an*p matrix of numbers, to plot a graph with n*p squares, each square having a colour depending of the number in the matrix. 我目前正在给定一个**数字矩阵,以n * p个正方形绘制一个图,每个正方形的颜色取决于矩阵中的数字。

The matrix is defined as follow: 矩阵定义如下:

ll <- list(c(1,3,4,3,6,5,8),c(1,1,4,5,7,6,8),c(1,3,1,1,3,4,8),c(2,1,1,2,1,3,5))
mm <- do.call(rbind,ll)

In a very general way, I would like to define colors for group of numbers. 通常,我想为数字组定义颜色。 For example: 例如:

  • Yellow for the group {1,2} 小组{1,2}黄色
  • Orange for the group {3,4,5} 小组{3,4,5}的橙色
  • Red for the gorup of numbers {6,7,8} 红色代表数字{6,7,8}

And then "plot" the matrix. 然后“绘制”矩阵。 Like the colorfull matplotlib picture on this link: http://activeintelligence.org/blog/archive/matplotlib-sparse-matrix-plot/ 就像此链接上的创辉matplotlib图片一样: http : //activeintelligence.org/blog/archive/matplotlib-sparse-matrix-plot/

I really have no clue how to do it, and any point of view would be greatly appreciated! 我真的不知道该怎么做,任何观点都将不胜感激!

 cc <- mm  # make copy to modify
 cc[] <- findInterval(cc, c(0, 2.5, 5.5, 8.5 ) )  # values 1:3
 cc
image(seq(dim(cc)[1]), seq(dim(cc)[2]), cc, col=c("yellow","orange","red"))

The values in the cc -matrix will pull from the color vector. cc -matrix中的值将从颜色向量中提取。

I suppose that the position of the points are defined by the n and p ranks. 我想这些点的位置由n和p等级定义。 You could handle this with ggplot2 and reshape2. 您可以使用ggplot2和reshape2处理此问题。

ll <- list(c(1,3,4,3,6,5,8),c(1,1,4,5,7,6,8),c(1,3,1,1,3,4,8),c(2,1,1,2,1,3,5))
mm <- do.call(rbind,ll)
rownames(mm) = 1:nrow(mm)
colnames(mm) = 1:ncol(mm)

library(reshape2)
library(ggplot2)
mm_long = melt(mm)
colnames(mm_long) = c("x", "y", "group")
mm_long$colour_group = NA
mm_long$colour_group[mm_long$group %in% c(1,2)] = 1
mm_long$colour_group[mm_long$group %in% c(3,4,5)] = 2
mm_long$colour_group[mm_long$group %in% c(6,7,8)] = 3

mm_long$group = factor(mm_long$group)
mm_long$colour_group = factor(mm_long$colour_group)

ggplot(mm_long, aes(x=x, y=y)) +
  geom_point(aes(colour=colour_group), shape=15, size=10) +
  scale_colour_manual(values = c("yellow","orange", "red"))

Basically following suggestions from @MrFlick & @BondedDust: 基本上遵循@MrFlick和@BondedDust的建议:

cols=c(rep("yellow", 2), rep("orange", 3), rep("red", 3))

image(1:ncol(mm), 1:nrow(mm), t(mm), col=cols, breaks=c(0:length(cols))+0.5, xlab="", ylab="")

or 要么

heatmap(mm, col=cols, breaks=c(0:length(cols))+0.5, Colv=NA, Rowv=NA, scale="none")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM