我感兴趣的是将矩阵图用作田间 map ,将矩阵中的某些位置用作特定的植物(田间位置).

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)

并获得:

enter image description here

问题在于,该地块似乎没有在正确的位置显示位置.

推荐答案

首先,你可以这样创建矩阵,

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

正如 comments 中已经提到的,sample个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个转座.我们最好使用自定义axisE来避免分数.

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

R相关问答推荐

列出用m n个值替换来绘制n个数字的所有方法(i.o.w.:R中大小为n的集合的所有划分为m个不同子集)

ggplot2中的X轴显示数值,单位为百,而不是十

迭代通过1个长度的字符串长字符R

R-更新面内部的栅格值

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

如何将R中数据帧中的任何Nas替换为最后4个值

R Select()可以测试不存在的子集列

无法定义沿边轨迹的 colored颜色 渐变(与值无关)

使用范围和单个数字将数字与字符串进行比较

Geom_Hline将不会出现,而它以前出现了

在数据帧列表上绘制GGPUP

按列中显示的配对组估算NA值

从数据创建数字的命名列表.R中的框

Geom_arcbar()中出错:找不到函数";geom_arcbar";

我将工作代码重构为一个函数--现在我想不出如何传递轴列参数

仅当后续值与特定值匹配时,才在列中回填Nas

快速合并R内的值

通过比较来自多个数据框的值和R中的条件来添加新列

使用相对风险回归计算RR

如何在给定的环境中找到函数的函数参数?