R:我们如何绘制棋盘(N×N)网格?

use*_*703 4 csv plot r ggplot2

如果我有这样的CSV:

row,column
1,0
5,1
7,2
2,3
0,4
3,5
6,6
4,7
Run Code Online (Sandbox Code Playgroud)

8x8棋盘

CSV数据中填充的黑色方块会导致:

填充8x8棋盘

我在底层的情节上绘制黑色方块.无法在右侧部分获得黑色方块.我还是R新手,所以我遇到了一些困难.我哪里错了?

library(data.table)

library(reshape2)
library(ggplot2)

data_csv <- fread('./data.csv')

mx <- matrix(data_csv, nrow=8, ncol=8)

ggplot(melt(mx), aes(x=Var1, y=Var2)) + geom_tile()
Run Code Online (Sandbox Code Playgroud)

试图使其动态化,以便如果CSV增长到n行,它仍将处理.

Gar*_*man 7

首先读入数据:

chessdat <- read.table(text='row,column
1,0
5,1
7,2
2,3
0,4
3,5
6,6
4,7', sep =',', header = T)
Run Code Online (Sandbox Code Playgroud)

因为geom_tile以点为中心,让我们给出一个偏移量

offset <- 0.5
chessdat2 <- chessdat + offset
Run Code Online (Sandbox Code Playgroud)

然后按照你的方式进行绘图:

ggplot(chessdat2, aes(row,column)) + geom_tile() + theme_bw()
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述

然后再玩格式,我们可以到棋盘:

ggplot(chessdat2, aes(row,column)) + geom_tile() + 
     theme_bw() + 
     theme(panel.grid.major = element_line(size = 2, color='black'),       
     panel.grid.minor = element_line(size=2, color = 'black'),
     axis.ticks = element_blank(), 
     axis.text = element_blank(), 
     axis.title = element_blank()) + 
     coord_cartesian(xlim=c(0,8), ylim=c(0,8))
Run Code Online (Sandbox Code Playgroud)

这给出了情节:

在此输入图像描述