在R中创建网格图片
Creating a grid picture in R
我想创建一个 10x10 网格图片(我不知道怎么称呼它最好),它查看 df 中的值并根据 df 中的值为相关网格着色。
例如,我有一个 df 如下(为简单起见,我使用 5x5 作为示例):
column <- c(rep(c(1),5),rep(c(2),5),rep(c(3),5),rep(c(4),5),rep(c(5),5))
row <- rep(1:5, 5)
ID <- c(11:15, 21:25, 31:35, 41:45, 51:55)
class <- c(0,0,1,2,1,2,2,3,0,1,2,3,1,2,0,1,0,0,2,3,3,2,2,2,1)
df <- data.frame(column, row, ID, class)
我想创建一个网格,根据 class
变量为每个点着色。
例如,对于第 1 行第 1 (ID = 11
),class 是 0
。在网格图片中,它将被涂成蓝色。 class 1
中的任何内容都将显示为绿色,class 2
将显示为红色,而 class 3
将显示为黄色。
它看起来像这样:
请原谅示例的简单性。
我可以将 df 保存为 .csv 和 运行 一个基于宏的工作簿 Excel 以获得所需的输出,但我希望能够在 R 中完成把所有东西都放在一个地方。
这是一张热图。你可以在 ggplot2:
library(ggplot2)
ggplot(df, aes(column, row, fill = as.factor(class))) +
geom_tile() +
scale_fill_manual(values = c("blue", "green", "red", "yellow"),
labels = 0:3, name = "class")
由 reprex package (v0.3.0)
于 2020-07-13 创建
我想创建一个 10x10 网格图片(我不知道怎么称呼它最好),它查看 df 中的值并根据 df 中的值为相关网格着色。
例如,我有一个 df 如下(为简单起见,我使用 5x5 作为示例):
column <- c(rep(c(1),5),rep(c(2),5),rep(c(3),5),rep(c(4),5),rep(c(5),5))
row <- rep(1:5, 5)
ID <- c(11:15, 21:25, 31:35, 41:45, 51:55)
class <- c(0,0,1,2,1,2,2,3,0,1,2,3,1,2,0,1,0,0,2,3,3,2,2,2,1)
df <- data.frame(column, row, ID, class)
我想创建一个网格,根据 class
变量为每个点着色。
例如,对于第 1 行第 1 (ID = 11
),class 是 0
。在网格图片中,它将被涂成蓝色。 class 1
中的任何内容都将显示为绿色,class 2
将显示为红色,而 class 3
将显示为黄色。
它看起来像这样:
请原谅示例的简单性。
我可以将 df 保存为 .csv 和 运行 一个基于宏的工作簿 Excel 以获得所需的输出,但我希望能够在 R 中完成把所有东西都放在一个地方。
这是一张热图。你可以在 ggplot2:
library(ggplot2)
ggplot(df, aes(column, row, fill = as.factor(class))) +
geom_tile() +
scale_fill_manual(values = c("blue", "green", "red", "yellow"),
labels = 0:3, name = "class")
由 reprex package (v0.3.0)
于 2020-07-13 创建