在R中可视化2变量联合概率质量函数

在R中可视化2变量联合概率质量函数,r,R,我在R中有一个矩阵,表示两个变量的联合(pmf),例如: > matrix(c(.13, .00004, 0, 0, 0, .04, .13, .008, 0, 0, .01, .007, .16, .02, .0004, .004, .025, .070, .14, .01, .001, .007, .028, .028, .12), nrow=5) [,1] [,2] [,3] [,4] [,5] [1,] 0.13000 0.040 0.0100 0.004

我在R中有一个矩阵,表示两个变量的联合(pmf),例如:

> matrix(c(.13, .00004, 0, 0, 0, .04, .13, .008, 0, 0, .01, .007, .16, .02, .0004, .004, .025, .070, .14, .01, .001, .007, .028, .028, .12), nrow=5)
        [,1]  [,2]   [,3]  [,4]  [,5]
[1,] 0.13000 0.040 0.0100 0.004 0.001
[2,] 0.00004 0.130 0.0070 0.025 0.007
[3,] 0.00000 0.008 0.1600 0.070 0.028
[4,] 0.00000 0.000 0.0200 0.140 0.028
[5,] 0.00000 0.000 0.0004 0.010 0.120

我想创建一个二维可视化的数据,作为一个正方形,分成5x5个较小的正方形,其中单个正方形的颜色与矩阵中的条目成比例。(在上述情况下,对角线上的颜色最暗)。有没有一种简单的方法来生成这种类型的图像

ggplot可以很容易地处理这个问题。我知道两种简单的方法:

library(ggplot2)
dat <- matrix(c(.13, .00004, 0, 0, 0, .04, .13, .008, 0, 0, .01, .007, .16, .02, .0004, .004, .025, .070, .14, .01, .001, .007, .028, .028, .12), nrow=5)
ggfluctuation(as.table(dat), type = "colour") +
    scale_fill_gradient(low = "white", high = "blue")

#Or with geom_tile
dat.m <- melt(dat)

ggplot(dat.m, aes(X1, X2, fill = value)) + 
    geom_tile(colour = "grey") + scale_fill_gradient(low = "white", high = "blue")
可以使用
image()
功能:

mat <- matrix(c(.13, .00004, 0, 0, 0, 
                .04, .13, .008, 0, 0,
                .01, .007, .16, .02, .0004,
                .004, .025, .070, .14, .01,
                .001, .007, .028, .028, .12), nrow=5)
image(mat, col = rev(heat.colors(12)))
mat试试这个:

library(lattice)

#Build the data
x <- matrix(c(.13, .00004, 0, 0, 0, .04, .13, .008, 0, 0, .01, .007, .16, .02, .0004, .004, .025, .070, .14, .01, .001, .007, .028, .028, .12), nrow=5)
xmin <- min(x)
xmax <- max(x)

#Build the plot
pal <- colorRampPalette(c("lightblue", "blue"), space = "rgb")
levelplot(x, main="5 X 5 Levelplot", xlab="", ylab="", col.regions=pal(120), cuts=100, at=seq(xmin, xmax, (xmax-xmin)/20))
库(晶格)
#构建数据

什么是“pmf”?你能在你的Q标题和文本中为那些不懂行话的人澄清这一点吗?@Gavin Simpson:我编辑了标题和文本,希望能更清楚一点。
library(lattice)

#Build the data
x <- matrix(c(.13, .00004, 0, 0, 0, .04, .13, .008, 0, 0, .01, .007, .16, .02, .0004, .004, .025, .070, .14, .01, .001, .007, .028, .028, .12), nrow=5)
xmin <- min(x)
xmax <- max(x)

#Build the plot
pal <- colorRampPalette(c("lightblue", "blue"), space = "rgb")
levelplot(x, main="5 X 5 Levelplot", xlab="", ylab="", col.regions=pal(120), cuts=100, at=seq(xmin, xmax, (xmax-xmin)/20))