如何用``ggplot2''从数字矩阵中绘制栅格

发布于 2025-01-26 03:16:56 字数 2613 浏览 4 评论 0原文

我希望根据数字矩阵创建一个刻面的栅格图。各个方面由矩阵中的行定义。

以下样本数据表示2 x 1个细胞形状(“形状”)可以水平和垂直放置在3 x 3网格上。

m1 <- matrix(c(1,0,0,1,0,0,0,0,0,
               0,0,0,1,0,0,1,0,0,
               0,1,0,0,1,0,0,0,0,
               0,0,0,0,1,0,0,1,0,
               0,0,1,0,0,1,0,0,0,
               0,0,0,0,0,1,0,0,1,
               1,1,0,0,0,0,0,0,0,
               0,1,1,0,0,0,0,0,0,
               0,0,0,1,1,0,0,0,0,
               0,0,0,0,1,1,0,0,0,
               0,0,0,0,0,0,1,1,0,
               0,0,0,0,0,0,0,1,1), ncol = 9, byrow = TRUE)

矩阵中的每一行代表形状的可能位置。这样,将行格式化为具有三个列的矩阵将说明位置(1 =占用的单元格,0 =无占用的单元格)。

matrix(m1[1,], ncol = 3, byrow = TRUE)
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    1    0    0
#> [3,]    0    0    0

创建显示的第一个位置是左上角的垂直位置。

我可以使用ggplot2使用anotated_customgrid :: rastergrob函数绘制此内容。

library(ggplot2)
library(grid)

m1 <- matrix(c(1,0,0,1,0,0,0,0,0,
               0,0,0,1,0,0,1,0,0,
               0,1,0,0,1,0,0,0,0,
               0,0,0,0,1,0,0,1,0,
               0,0,1,0,0,1,0,0,0,
               0,0,0,0,0,1,0,0,1,
               1,1,0,0,0,0,0,0,0,
               0,1,1,0,0,0,0,0,0,
               0,0,0,1,1,0,0,0,0,
               0,0,0,0,1,1,0,0,0,
               0,0,0,0,0,0,1,1,0,
               0,0,0,0,0,0,0,1,1), ncol = 9, byrow = TRUE)

#create a raster grob object from the first row of the matrix  

g <- grid::rasterGrob(matrix(m1[1,], ncol = 3, byrow = TRUE),
                      interpolate = FALSE)

#adjust the colours
g$raster[g$raster == "#FFFFFF"] <- "#0000FF"
g$raster[g$raster == "#000000"] <- "#FFFFFF"

g$raster
#>      [,1]      [,2]      [,3]     
#> [1,] "#0000FF" "#FFFFFF" "#FFFFFF"
#> [2,] "#0000FF" "#FFFFFF" "#FFFFFF"
#> [3,] "#FFFFFF" "#FFFFFF" "#FFFFFF"

ggplot() +
  annotation_custom(g) +
  coord_equal() +
  theme(
    panel.border = element_rect(colour = "#000000", fill = "transparent")
  )

“”

reprex软件包(v2.0.1)

一次适合一个位置。

但是,我的目标是为每行(位置)绘制一个面板,以便我可以一起查看它们。

我正在使用它来说明根据提供的网格大小和形状长度创建初始矩阵的函数的输出。因此,该解决方案必须足够灵活,以使最初矩阵中的可变大小和复杂性可容纳。

我出于习惯而使用ggplot2,但我对其他方法开放。

问题:

我如何创建一个刻面图,每个面板代表矩阵的一行,并以栅格的格式格式化?

I wish to create a facetted raster plot based on a numeric matrix. The facets are defined by rows in the matrix.

The following sample data represents all the ways a 2 x 1 cell shape (the "Shape") may be placed horizontally and vertically on a 3 x 3 grid.

m1 <- matrix(c(1,0,0,1,0,0,0,0,0,
               0,0,0,1,0,0,1,0,0,
               0,1,0,0,1,0,0,0,0,
               0,0,0,0,1,0,0,1,0,
               0,0,1,0,0,1,0,0,0,
               0,0,0,0,0,1,0,0,1,
               1,1,0,0,0,0,0,0,0,
               0,1,1,0,0,0,0,0,0,
               0,0,0,1,1,0,0,0,0,
               0,0,0,0,1,1,0,0,0,
               0,0,0,0,0,0,1,1,0,
               0,0,0,0,0,0,0,1,1), ncol = 9, byrow = TRUE)

Each row in the matrix represents a possible position for the Shape. Such that, formatting a row as a matrix with three columns will illustrate the position (1 = occupied cell and 0 = unoccupied cell).

matrix(m1[1,], ncol = 3, byrow = TRUE)
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    1    0    0
#> [3,]    0    0    0

Created on 2022-05-03 by the reprex package (v2.0.1)

As shown, the first position is a vertical placement in the top left corner.

I can plot this with ggplot2 using anotated_custom and the grid::rasterGrob function.

library(ggplot2)
library(grid)

m1 <- matrix(c(1,0,0,1,0,0,0,0,0,
               0,0,0,1,0,0,1,0,0,
               0,1,0,0,1,0,0,0,0,
               0,0,0,0,1,0,0,1,0,
               0,0,1,0,0,1,0,0,0,
               0,0,0,0,0,1,0,0,1,
               1,1,0,0,0,0,0,0,0,
               0,1,1,0,0,0,0,0,0,
               0,0,0,1,1,0,0,0,0,
               0,0,0,0,1,1,0,0,0,
               0,0,0,0,0,0,1,1,0,
               0,0,0,0,0,0,0,1,1), ncol = 9, byrow = TRUE)

#create a raster grob object from the first row of the matrix  

g <- grid::rasterGrob(matrix(m1[1,], ncol = 3, byrow = TRUE),
                      interpolate = FALSE)

#adjust the colours
g$raster[g$raster == "#FFFFFF"] <- "#0000FF"
g$raster[g$raster == "#000000"] <- "#FFFFFF"

g$raster
#>      [,1]      [,2]      [,3]     
#> [1,] "#0000FF" "#FFFFFF" "#FFFFFF"
#> [2,] "#0000FF" "#FFFFFF" "#FFFFFF"
#> [3,] "#FFFFFF" "#FFFFFF" "#FFFFFF"

ggplot() +
  annotation_custom(g) +
  coord_equal() +
  theme(
    panel.border = element_rect(colour = "#000000", fill = "transparent")
  )

Created on 2022-05-03 by the reprex package (v2.0.1)

This works fine for one position at a time.

However, my objective is to plot a panel for each row (position) so I can view them all together.

I am using this to illustrate the output of a function that creates the initial matrix based on Grid size and Shape lengths provided. So the solution must be flexible enough to accomodate variable size and complexity in the initial matrix.

I am using ggplot2 out of habit, but I am open to other approaches.

Question:

How do I create a facetted plot, where each panel represents a row from a matrix, and formatted as a raster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-02-02 03:16:56

您需要将矩阵重塑为适当的数据框架:

df <- data.frame(fill = factor(c(t(m1))),
                 row = factor(rep(rep(1:3, each = 3), len = length(m1)), 3:1),
                 col = factor(rep(1:3, len = length(m1))),
                 fac = factor(rep(seq(nrow(m1)), each = ncol(m1))))

然后绘图变得直接:

ggplot(df, aes(col, row, fill = fill)) +
  geom_tile() +
  facet_wrap(.~fac) +
  coord_equal() +
  scale_fill_manual(values = c(`1` = "navy", `0` = "white"))

”在此处输入图像描述”

You need to reshape your matrix into an appropriate data frame:

df <- data.frame(fill = factor(c(t(m1))),
                 row = factor(rep(rep(1:3, each = 3), len = length(m1)), 3:1),
                 col = factor(rep(1:3, len = length(m1))),
                 fac = factor(rep(seq(nrow(m1)), each = ncol(m1))))

Then the plot becomes straightforward:

ggplot(df, aes(col, row, fill = fill)) +
  geom_tile() +
  facet_wrap(.~fac) +
  coord_equal() +
  scale_fill_manual(values = c(`1` = "navy", `0` = "white"))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文