1
votes

I have a data array, a, shown below:

a <- structure(c(0.9984951, 0.9959022, 0.9888077, 0.9691369, 0.9110772, 
0.001501177, 0.004070025, 0.010984665, 0.029266702, 0.075221083, 
0.999987, 0.9999417, 0.9997387, 0.9988276, 0.9947208, 1.300713e-05, 
5.829127e-05, 0.0002611903, 0.001169507, 0.005219817, 0.9999757, 
0.9999152, 0.9997039, 0.9989658, 0.996382, 2.430024e-05, 8.481104e-05, 
0.0002959571, 0.001032229, 0.003593514), .Dim = c(5L, 2L, 3L))

dim(a)
[1] 5 2 3

a
, , 1
          [,1]        [,2]
[1,] 0.9984951 0.001501177
[2,] 0.9959022 0.004070025
[3,] 0.9888077 0.010984665
[4,] 0.9691369 0.029266702
[5,] 0.9110772 0.075221083

, , 2

          [,1]         [,2]
[1,] 0.9999870 1.300713e-05
[2,] 0.9999417 5.829127e-05
[3,] 0.9997387 2.611903e-04
[4,] 0.9988276 1.169507e-03
[5,] 0.9947208 5.219817e-03

, , 3

          [,1]         [,2]
[1,] 0.9999757 2.430024e-05
[2,] 0.9999152 8.481104e-05
[3,] 0.9997039 2.959571e-04
[4,] 0.9989658 1.032229e-03
[5,] 0.9963820 3.593514e-03

I have another vector that is the same length as the first dimension of the data array:

b = c(-1, -0.5, 0, 0.5, 1)

Is there a way in r to plot the data so that I can get 3 separate plots (this number equals the third dimensions of the array and each plot has 2 lines on it (this number equals the second dimensions of the array). And each line is the second dimension values as the y axis and the vector b as the x-axis, so the first data block should be the first plot, and two columns indicate two lines, and each column is the y values, and the corresponding x values are included in the b vector. Thank you in advance for your help.

2
What are the dimensions of a (dim(a))? It look's like your subsetting a with a[5, 2, 3], but the output looks like it could be print(a). - Ian Campbell
oh the output is print(a), so the dim(a) is c(5, 2, 3) - Rlearner

2 Answers

1
votes

Since you asked for a ggplot solution, let's go all in with Tidyverse.

First, let's convert our 3 dimensional array into a list of data.frames:

my.dfs <- apply(a,3,as.data.frame)

Data frame columns must be named in R, so they will get the defaults of c("V1","V2"). You could set them to something differently in the apply call if you wanted.

Now let's use purrr::map2 to bind together the data from the array, together with a plot number and your b vector

library(tidyvers)
data <- map2_dfr(my.dfs,seq_along(my.dfs),~cbind(.x,plot = .y, b)) 

For plotting, we'll need to pivot the data into long form:

data <- pivot_longer(data, -c(plot,b), names_to = "variable")

And finally we can use the col = asethetic to change the line color by variable and use facet_wrap to make multiple plots.

ggplot(data, aes(x = b, y = value, col = variable)) +
  geom_line() +
  facet_wrap(~plot)

enter image description here

0
votes

You should consider:

d <- dim(a)
data.frame( x = b, y = c(a), plots = gl(d[3], prod(d[-3])), cols = gl(d[2], d[1])) %>%
    ggplot(aes(x, y, col = cols)) +
    geom_line() +
    facet_wrap(~plots)

enter image description here