根据标签的网格的颜色正方形
我已经使用以下代码创建了100x100网格:
nb=100
nb1=nb
nb2=nb
minlat=45.18306
maxlat=45.6325
binlat=seq(minlat-0.05, maxlat+0.05, length.out = nb1)
minlon=8.724444
maxlon=9.519722
binlon=seq(minlon-0.05, maxlon+0.05, length.out = nb2)
我的数据如下:
n = 100
coord = matrix(0, n, 2)
coord[,1]= runif(100, min=45.18306, max=45.6325)
coord[,2]= runif(100, min=8.724444, max=9.519722)
lab=matrix(0, n, 1)
m=1
for(i in 1:nb1){
for(j in 1:nb2){
lab[coord[,1]>binlat[i] & coord[,1]<=binlat[i+1] & coord[,2]>binlon[j] & coord[,2]<=binlon[j+1]]=m
m=m+1
}
}
假设我有一个如下:
a <- c(1000,2536,3045,6654)
b <- c(1,1,2,3)
matrix <- cbind(a,b)
where a
是向量识别网格平方ID和b
是广场所属的组的向量。
是否可以根据a
根据其属于的组彩色的平方可视化网格? a
中未包含的所有正方形必须是非彩色/白色的。
I have created a 100x100 grid using the following code:
nb=100
nb1=nb
nb2=nb
minlat=45.18306
maxlat=45.6325
binlat=seq(minlat-0.05, maxlat+0.05, length.out = nb1)
minlon=8.724444
maxlon=9.519722
binlon=seq(minlon-0.05, maxlon+0.05, length.out = nb2)
And my data is grouped like follows:
n = 100
coord = matrix(0, n, 2)
coord[,1]= runif(100, min=45.18306, max=45.6325)
coord[,2]= runif(100, min=8.724444, max=9.519722)
lab=matrix(0, n, 1)
m=1
for(i in 1:nb1){
for(j in 1:nb2){
lab[coord[,1]>binlat[i] & coord[,1]<=binlat[i+1] & coord[,2]>binlon[j] & coord[,2]<=binlon[j+1]]=m
m=m+1
}
}
Assuming I have a matrix like the following:
a <- c(1000,2536,3045,6654)
b <- c(1,1,2,3)
matrix <- cbind(a,b)
where a
is the vector identifying the grid square ID and b
is the vector of the group to which the square belongs.
Is it possible to visualize the grid with the squares in a
colored according to the group they belong to?
All the squares not included in a
must be non-colored/white.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
explion.grid
获取所有构成网格的唯一x和y值的数据框架。这将为您提供每个y值的所有x值,以下是:
...等于10,000行。
组标记正确的单元格,请访问:
如果您的
a
列用此订购代表单元格的矩阵位置,则可以通过执行以下的explion.grid
中的参数绘制结果可能最容易使用
ggplot
You can use
expand.grid
to get a data frame of all the unique x and y values making up a grid.This will give you all the x values at each y value in order:
...and so on for 10,000 rows.
If your
a
column represents the matrix position of a cell with this ordering, then you can label the correct cell with a group by doing:(if this is transposed from what you intended, simply reverse the order of the arguments in
expand.grid
)Plotting the result is probably easiest using
ggplot