根据标签的网格的颜色正方形

发布于 2025-01-24 00:54:10 字数 909 浏览 3 评论 0原文

我已经使用以下代码创建了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 技术交流群。

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

发布评论

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

评论(1

人生戏 2025-01-31 00:54:10

您可以使用explion.grid获取所有构成网格的唯一x和y值的数据框架。

df <- expand.grid(x = binlon, y = binlat)

这将为您提供每个y值的所有x值,以下是:

head(df)
#>          x        y
#> 1 8.674444 45.13306
#> 2 8.683487 45.13306
#> 3 8.692530 45.13306 
#> 4 8.701574 45.13306
#> 5 8.710617 45.13306
#> 6 8.719660 45.13306

...等于10,000行。

组标记正确的单元格,请访问:

df$group <- NA_real_
df$group[matrix[,'a']] <- matrix[,'b']
df$group <- factor(df$group)

如果您的a列用此订购代表单元格的矩阵位置,则可以通过执行以下的 explion.grid中的参数

绘制结果可能最容易使用ggplot

library(ggplot2)

ggplot(df, aes(x, y, fill = group)) + 
  geom_tile(colour = 'gray90') +
  scale_fill_brewer(palette = 'Set1', na.value = 'white')

“

You can use expand.grid to get a data frame of all the unique x and y values making up a grid.

df <- expand.grid(x = binlon, y = binlat)

This will give you all the x values at each y value in order:

head(df)
#>          x        y
#> 1 8.674444 45.13306
#> 2 8.683487 45.13306
#> 3 8.692530 45.13306 
#> 4 8.701574 45.13306
#> 5 8.710617 45.13306
#> 6 8.719660 45.13306

...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:

df$group <- NA_real_
df$group[matrix[,'a']] <- matrix[,'b']
df$group <- factor(df$group)

(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

library(ggplot2)

ggplot(df, aes(x, y, fill = group)) + 
  geom_tile(colour = 'gray90') +
  scale_fill_brewer(palette = 'Set1', na.value = 'white')

enter image description here

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