使用矩阵图(MATPLOT)作为地图和位置作为位置

发布于 2025-01-28 15:22:01 字数 679 浏览 1 评论 0原文

我有兴趣将矩阵图用作现场图,而矩阵中的某些位置作为特定植物(现场内的位置)。 我正在使用此数据:

Field<-matrix(1:1240,nrow = 40,ncol = 31)
SampTreatment<- sample(Field,6,replace = F) # Results: 388 155 582 405 1173 165
SampControl<- sample(Field,6,replace = F) # Results: 848 270 1159 1050 1177 1184

我正在尝试使用:

matplot(Field,col = 1,pch = 1,lty = 1)
points(SampControl,col= 'blue',pch=19,cex=1.5)
points(SampTreatment,col= 'red',pch=17,cex=1.5)

“在此处输入图像说明”

问题是,该图似乎没有在正确的位置显示位置。

I'm interested in using a matrix plot as a field map and certain locations in matrix as specific plants (locations within field).
I'm using this data:

Field<-matrix(1:1240,nrow = 40,ncol = 31)
SampTreatment<- sample(Field,6,replace = F) # Results: 388 155 582 405 1173 165
SampControl<- sample(Field,6,replace = F) # Results: 848 270 1159 1050 1177 1184

I'm trying to plot it using:

matplot(Field,col = 1,pch = 1,lty = 1)
points(SampControl,col= 'blue',pch=19,cex=1.5)
points(SampTreatment,col= 'red',pch=17,cex=1.5)

and getting:

enter image description here

The issue is that it seems the plot is not showing locations in the right place.

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

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

发布评论

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

评论(1

如何视而不见 2025-02-04 15:22:01

首先,您可能需要像这样创建矩阵,

n <- 6; m <- 4
(field <- matrix(seq_len(n), n, m))
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    2    2    2    2
# [3,]    3    3    3    3
# [4,]    4    4    4    4
# [5,]    5    5    5    5
# [6,]    6    6    6    6

并且如注释中所建议的,示例 x和y坐标。只需将过程包裹在功能中。

sampfun <- \(size, mat) {
  stopifnot(size*2 <= prod(dim(mat)))
  s <- matrix(,size*2, 2, dimnames=list(NULL, c('x', 'y')))
  for (i in seq_len(size*2)) {
    repeat {
      x <- sample(ncol(mat), 1, replace=TRUE)
      y <- sample(nrow(mat), 1, replace=TRUE)
      s[i, ] <- cbind(x, y)
      if (!any(duplicated(s[1:i,,drop=FALSE]))) break
    }
  }
  return(list(treated=s[1:size, ], control=s[-(1:size), ]))
}

set.seed(42)
(samp <- sampfun(6, field))
# $treated
#      x y
# [1,] 1 5
# [2,] 1 1
# [3,] 2 4
# [4,] 2 2
# [5,] 1 4
# [6,] 3 3
# 
# $control
#      x y
# [1,] 3 4
# [2,] 4 3
# [3,] 2 1
# [4,] 2 6
# [5,] 3 6
# [6,] 4 6

现在请注意,matplot转台,因此您还需要t翻转。更好的是,我们使用自定义 es避免分数。

png('test.png')

matplot(t(field), col=1, pch=1, xlab='x', ylab='y', main='Field', xaxt='n', yaxt='n')
axis(1, seq_len(ncol(field))); axis(2, seq_len(nrow(field)))
points(samp$control, col='blue', pch=19, cex=1.5)
points(samp$treated, col='red', pch=17, cex=1.5)
## optional legend
legend(par()$usr[1], par()$usr[3] - .5, legend=c('treated', 'control'), 
       col=c('red', 'blue'), pch=c(17, 19), bty='n', horiz=TRUE, cex=.9, xpd=TRUE)   

dev.off()

First, you may want to create your matrix like so,

n <- 6; m <- 4
(field <- matrix(seq_len(n), n, m))
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    2    2    2    2
# [3,]    3    3    3    3
# [4,]    4    4    4    4
# [5,]    5    5    5    5
# [6,]    6    6    6    6

and, as already suggested in comments, sample x and y coordinates. Just wrap the process in a small function.

sampfun <- \(size, mat) {
  stopifnot(size*2 <= prod(dim(mat)))
  s <- matrix(,size*2, 2, dimnames=list(NULL, c('x', 'y')))
  for (i in seq_len(size*2)) {
    repeat {
      x <- sample(ncol(mat), 1, replace=TRUE)
      y <- sample(nrow(mat), 1, replace=TRUE)
      s[i, ] <- cbind(x, y)
      if (!any(duplicated(s[1:i,,drop=FALSE]))) break
    }
  }
  return(list(treated=s[1:size, ], control=s[-(1:size), ]))
}

set.seed(42)
(samp <- sampfun(6, field))
# $treated
#      x y
# [1,] 1 5
# [2,] 1 1
# [3,] 2 4
# [4,] 2 2
# [5,] 1 4
# [6,] 3 3
# 
# $control
#      x y
# [1,] 3 4
# [2,] 4 3
# [3,] 2 1
# [4,] 2 6
# [5,] 3 6
# [6,] 4 6

Now note, that matplot transposes, so you also need to transpose. Better we use custom axises to avoid fractions.

png('test.png')

matplot(t(field), col=1, pch=1, xlab='x', ylab='y', main='Field', xaxt='n', yaxt='n')
axis(1, seq_len(ncol(field))); axis(2, seq_len(nrow(field)))
points(samp$control, col='blue', pch=19, cex=1.5)
points(samp$treated, col='red', pch=17, cex=1.5)
## optional legend
legend(par()$usr[1], par()$usr[3] - .5, legend=c('treated', 'control'), 
       col=c('red', 'blue'), pch=c(17, 19), bty='n', horiz=TRUE, cex=.9, xpd=TRUE)   

dev.off()

enter image description here

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