[R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

jim holtman jholtman at gmail.com
Sun Sep 22 17:10:41 CEST 2013


Is this what you were after:


> ## A function to generate a RCB design
>
> rcbd<-function(b,g,rb,cb,r,c)
+ {
+    # b =number of blocks
+    # g = a vector of treatments
+    # rb = number of rows per blocks
+    # cb =number of columns per block
+    # r = total rows
+    # c = total columns
+ library(foreach)
+ genotypes<-times(b) %do% sample(g,length(g))
+ block<-rep(1:b,each=length(g))
+ genotypes<-factor(genotypes)
+ block<-factor(block)
+ ### generate the base design
+ k<-c/cb # number of blocks on the x-axis
+ x<-rep(rep(1:r,each=cb),k)  # X-coordinate
+ l<-cb
+ p<-r/rb
+ m<-l+1
+ d<-l*b/p
+ y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
+ data.frame(x,y,block,genotypes)
+ }
> set.seed(100)
> ans <- rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)
>
> result <- matrix(nrow = max(ans$x), ncol = max(ans$y))
> result[cbind(ans$y, ans$x)] <- ans$genotypes
> result
     [,1] [,2] [,3] [,4]
[1,]    2    4    2    3
[2,]    1    3    4    1
[3,]    3    2    2    3
[4,]    1    4    4    1
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Sep 22, 2013 at 1:07 AM, Laz <lmramba at ufl.edu> wrote:
> Dear R users,
>
> I have a function I created to generate randomized block designs given
> below. Once  I generate the design, I would like to plot it in a
> rectangular form to mimic the excel style where the blocks are
> represented with the treatment values placed exactly where their
> appropriate X and Y coordinates are.
>
> ## A function to generate a RCB design
>
> rcbd<-function(b,g,rb,cb,r,c)
> {
>    # b =number of blocks
>    # g = a vector of treatments
>    # rb = number of rows per blocks
>    # cb =number of columns per block
>    # r = total rows
>    # c = total columns
> library(foreach)
> genotypes<-times(b) %do% sample(g,length(g))
> block<-rep(1:b,each=length(g))
> genotypes<-factor(genotypes)
> block<-factor(block)
> ### generate the base design
> k<-c/cb # number of blocks on the x-axis
> x<-rep(rep(1:r,each=cb),k)  # X-coordinate
> l<-cb
> p<-r/rb
> m<-l+1
> d<-l*b/p
> y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
> data.frame(x,y,block,genotypes)
> }
> set.seed(100)
> rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)
>
>     x y block genotypes
> 1  1 1     1         2
> 2  1 2     1         1
> 3  2 1     1         4
> 4  2 2     1         3
> 5  3 1     2         2
> 6  3 2     2         4
> 7  4 1     2         3
> 8  4 2     2         1
> 9  1 3     3         3
> 10 1 4     3         1
> 11 2 3     3         2
> 12 2 4     3         4
> 13 3 3     4         2
> 14 3 4     4         4
> 15 4 3     4         3
> 16 4 4     4         1
>
> How can I produce a diagram like this one below or a better one for any run of my function?
>
>
> 2       4       2       3
> 1       3       4       1
> 3       2       2       3
> 1       4       4       1
>
>
> Regards,
> Laz
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list