2

I have a data matrix with a 525*95 dimension. The data contains simulated values that I wish to plot in the same graph. This means that I have 95 different variables that all have the same starting point on the Y-axis but will take different paths on the X-axis.

I'm looking for a way to plot all of the simulated values (stored in the columns) individually but in the same graph. The command plot() seems to assume that I wish to plot the simulated paths against each other but I'm only interested in plotting them against the number of periods they have been simulated for, namely 525.

Any clues?

           V1         V2         V3
1  0.01076000 0.01076000 0.01076000
2  0.01135591 0.01081002 0.01081920
3  0.01117306 0.01034507 0.01079422
4  0.01090997 0.01091913 0.01065135
5  0.01072611 0.01082653 0.01091554
6  0.01121228 0.01098110 0.01149064
7  0.01171061 0.01138791 0.01282230
8  0.01057508 0.01208230 0.01268310
9  0.01033449 0.01172448 0.01233295
10 0.01067395 0.01297883 0.01247032
user2182066
  • 157
  • 2
  • 9
  • 2
    Could you show a reproducible example of your data? (You could get this by doing `dput` on the first few rows or first few columns). This would make your question clearer and make it easier to answer. – David Robinson May 06 '13 at 14:57

2 Answers2

1

One easy way to do this is with ggplot2. You just need to reshape your data into a long format, which you can do very easily with the package reshape. An example with some simulated data:

## Simulate some data
set.seed(123)
df <- data.frame( y = runif(10) + 2 , x = runif(10) + 4 , z = runif(10) + 5 )
df
#         y        x        z
#1  2.287578 4.956833 5.889539
#2  2.788305 4.453334 5.692803
#3  2.408977 4.677571 5.640507
#4  2.883017 4.572633 5.994270
#5  2.940467 4.102925 5.655706
#6  2.045556 4.899825 5.708530
#7  2.528105 4.246088 5.544066
#8  2.892419 4.042060 5.594142
#9  2.551435 4.327921 5.289160
#10 2.456615 4.954504 5.147114


## Make an ID variable to use as the x-axis
df$ID <- 1:nrow(df)

## Melt data into long format 
df.long <- melt( df , id.vars = "ID" )


## Plot using the column name as the grouping variable
## After melting the column names are stored in the 
## column called 'varaible'
ggplot( df.long , aes( x = ID , y = value , group = variable ) ) + geom_line( aes( color = variable  ) , stat = "smooth" )

enter image description here

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Thank you for your answers. However I when I try it, I get this error message. Error in initFields(scales = scales) : could not find function "initRefFields" – user2182066 May 06 '13 at 15:54
  • @user2182066 You need to do as DavidRobinson asked and show an example of your data. I can also only help if I know the structure of your data and the exact code you used to try to plot it. Did you read my example and try to understand what each line does? You won't be able to apply it to your data without making a few adjustments. – Simon O'Hanlon May 06 '13 at 15:59
  • @user2182066 when you run `packageVersion("ggplot2")` what do you get? – Simon O'Hanlon May 06 '13 at 16:00
  • I don't quite understand how to paste my data into the thread without it looking messy. How does the dput command work? Yes I understand your code and have changed it to fit my data, as far as I understand. When i run the command packageVerision("ggplot2") i get‘0.9.1’ – user2182066 May 06 '13 at 16:08
  • Hmm. I get `[1] ‘0.9.3.1’`. You could try reinstalling `ggplot2` and also starting from a fresh session of R. To use `dput` just run say `dput( mydf[1:10,1:5] )` and it will output some code that if you paste it into your question we can copy and paste into our R session to reconstruct the first 10 rows and 5 columns of your data. – Simon O'Hanlon May 06 '13 at 16:13
  • @user2182066 what version of R are you using? Paste `R.version.string` into the console. – Simon O'Hanlon May 06 '13 at 16:18
  • I have re-installed ggplot2 but according to my R verision, which is R version 2.14.0 (2011-10-31), the latest ggplot is 0.9.1. I have now pasted some code into the question above. I can post more rows and colums if you wish. – user2182066 May 06 '13 at 16:42
  • @user2182066 I recommend you upgrade R to at least `2.15.2`. You probably won't have to reinstall many packages (like you would with R 3.0.0), but if you do you can do this easily with `update.pacakges(checkBuilt = TRUE)` although it can take some time if you have a very large package library. See [**here**](http://stackoverflow.com/q/9863604/1478381) – Simon O'Hanlon May 06 '13 at 16:46
1

You could also use matplot.

Here an example with some sample data (similar to the data of SimonO101 but with dimension 525*95):

df <- as.data.frame(matrix(runif(525 * 95), ncol=95))
for (i in 1:ncol(df))  
  df[,i]=df[,i]+i
df[1,] = rep(0, 95)

and then plot:

matplot(1:nrow(df), df, type="l")

enter image description here

user1981275
  • 13,002
  • 8
  • 72
  • 101