R: Make a array baseplot and color by array dim levels

Viewed 32

Hi I want to know how to color a baseplot of array data for different array levels (dimnames 1, 2 and 3)

x <- rnorm(1000); dim(x) <- c(10,10,10)

dimnames(x) = list(c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
                   c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
                   c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
plot(x, col=as.factor(dimnames(x)[[3]]))

didn't give me the wanted results as...

plot(x[1:100], col=as.factor(dimnames(x)[[3]]))

didn't give me one color for the first 100 numbers

I want to know how I can color the base plot by the different sets over the dimensions in baseplot.

3 Answers

You need to get back the array index, i.e which sub-array the datapoint belongs to and provide something as long as your data points to plot, for example:

plotfunc = function(this_array,index_to_plot){
array_index = 1+(index_to_plot - 1) %/% prod(dim(this_array)[1:2])
COLS=as.factor(dimnames(this_array)[[3]])[array_index]
plot(this_array[index_to_plot],col=COLS)
}

If you are plotting 1:100 from array x your provided, you are plotting the first sub-array, and all's black:

plotfunc(x,1:100)

enter image description here

We can try from the first 50 from the first 3 sub-arrays:

plotfunc(x,c(1:50,101:150,201:250))

enter image description here

I think I got what I wanted, at least with a 10x10x10 array, I have to validate it for different array dimensions. Also make it more strict by making sure only 1,2 or 3 can be choosen for array_level.

plotfunc2 = function(this_array,index_to_plot,array_level)
if (array_level == 1){
  plot(this_array[index_to_plot])
} else if (array_level == 2){
  array_index = 1+(index_to_plot - 1) %/% prod(dim(this_array)[1:2])
  COLS=as.factor(dimnames(this_array)[[3]])[array_index]
  plot(this_array[index_to_plot],col=COLS)
} else if (array_level == 3){
  array_index = rep(rep(c(1:dim(this_array)[3]), each = dim(this_array)[3]), times = dim(this_array)[3])
  COLS=as.factor(dimnames(this_array)[[2]])[array_index]
  plot(this_array[index_to_plot],col=COLS)
}

plotfunc2(x,1:500,3) gives

enter image description here

Thanks for the input

I tried to adjust the function in order to select the level of the array I want the colors to be taken over. But I can't seem to get the lowest level right since it takes only the factor levels over 1:10 and if I go on the lowest array level (sets of 10) I can't figure out to get the formula right.

plotfunc2 = function(this_array,index_to_plot,array_level)
if (array_level == 1){
  plot(this_array[index_to_plot])
} else if (array_level == 2){
  array_index = 1+(index_to_plot - 1) %/% prod(dim(this_array)[1:2])
  COLS=as.factor(dimnames(this_array)[[3]])[array_index]
  plot(this_array[index_to_plot],col=COLS)
} else if (array_level == 3){
  array_index = 1+(index_to_plot - 1) %/% dim(this_array)[3]
  COLS=as.factor(dimnames(this_array)[[2]])[array_index]
  plot(this_array[index_to_plot],col=COLS)
}

enter image description here

Related