0

I need to plot many graphs with all the combination of the elemts of a matrix.

I have three columns. I would like to plot the time series of the elements named A corresponding to W1, then the time series of the elements named B corresponding to W1. Same with W2; In reality my matrix is huge, but the idea should apply.

In particular, I would like to learn how to tell R to group together all the elements of W1 separating them by the elements of name.

Furthermore, what is the most efficient way to plot all these graphs (in the example below, I would need 4 graphs)?

name    W1   W2
A       123  24
A       754  7245
A       475  257
A       623  2457
A       525  27
A       256  72
A       352  725
B       257  572
B       572  577
B       75   257
B       27   257
B       247  7522
B       257  724

1 Answers1

0

I use ggplot2 package for plotting, and the reshape2 package for converting wide data to long data.

# get x values for your plots
df$x = ave(df$W1, df$name, FUN = seq_along)
# conver wide data to long
df_long = reshape2::melt(df, id.vars = c("name", "x"))

library(ggplot2)
# plot
ggplot(df_long, aes(x = x, y = value)) + geom_line() +
  facet_grid(name ~ variable)

enter image description here


Using this data:

df = read.table(text = "name    W1   W2
A       123  24
A       754  7245
A       475  257
A       623  2457
A       525  27
A       256  72
A       352  725
B       257  572
B       572  577
B       75   257
B       27   257
B       247  7522
B       257  724", header= T)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks! Is there also a way to get the four vectors in four separated objects? Such as vector_A_W1 = [123, 754,...]. – Lapsang Souchong Nov 15 '17 at 19:45
  • Sure, look at `?split`, you can use it on the long data in my question. I'd strongly recommend keeping the vectors in a `list` (as `split` will do by default), but if you really want separate objects you can use `list2env` on the list to put them in your workspace. – Gregor Thomas Nov 15 '17 at 19:47
  • I have another question: in reality I have to plot hundreds of graph for each combination. The number varies since I have different datasets (with same structure). I would like to feed R with the dataset, and make him plotting the graphs automatically. Is it possible to do that? I am sorry for asking such a question, but I do not really know where to start.. – Lapsang Souchong Nov 16 '17 at 17:28
  • Yes, it's possible. Make a [small reproducible example (see this excellent guide)](https://stackoverflow.com/q/5963269/903061) and ask another question. Be specific about how you want the outputs (saved to files? one graph per file? Or a grid of graphs for a whole dataset, like in my answer here, per file? Maybe also read through my answer at [How to make a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207), it is a good place to start for working with multiple data sets with the same structure. – Gregor Thomas Nov 16 '17 at 17:42
  • thank you these guides provides info for what I was looking for! – Lapsang Souchong Nov 16 '17 at 18:17