0

I am building an R function that would plot multiple Force Vs. Displacement lines in one graph. Here is my set of data:

       myrows displacememnt force displacememnt force displacememnt force displacememnt force displacememnt
[1,]      1  0.000000e+00     0  0.000000e+00     0  0.000000e+00     0  0.000000e+00     0  0.000000e+00
[2,]      2  4.292647e-08     0  8.829379e-08     0  4.194021e-08     0  4.136511e-08     0 -3.818898e-08
[3,]      3  1.284946e-09     0  1.343980e-07     0  4.665416e-08     0  3.375577e-08     0 -4.819687e-09
[4,]      4  4.380121e-08     0  1.742593e-07     0  7.184801e-09     0  3.734106e-08     0  3.210356e-08
[5,]      5  8.224440e-08     0  2.558701e-07     0  1.245641e-07     0  8.266907e-08     0  1.024775e-07
[6,]      6  1.269747e-07     0  3.842161e-07     0  1.223706e-07     0  1.666898e-07     0  1.407497e-07 

All I want to do is plot multiple force vs. displacement within the same graph. But I dont know how to split up the data , I tried melt function:

    meltedData <- melt(dataset,id.vars="myrows")
> head(meltedData)
  Var1   Var2 value
1    1 myrows     1
2    2 myrows     2
3    3 myrows     3
4    4 myrows     4
5    5 myrows     5
6    6 myrows     6

This is the minimal reprsentation of the dataset.

Would anyone be able to assist me to acheive my objective i.e. to plot multiple force v displacement lines within one plot?

Final plot what it looks like:

enter image description here

4
  • You can use the plot() function to plot the first series, and use lines() or points() to add subsequent series Commented Sep 22, 2014 at 15:10
  • would be a lot easier if there was a ggplot function that I can tell by splitting two columns at a time (force v disp)? Commented Sep 22, 2014 at 15:12
  • I think matplot is what you want. Commented Sep 22, 2014 at 15:20
  • btw, posting the output from dput(dataset) would make it easier to reproduce your example. Commented Sep 22, 2014 at 15:37

1 Answer 1

2

Provided your data frame has the same format you show in the example, you can break it into a long data frame with this:

df <- data.frame(myrows=c(1,2,3), force=c(0, 0.1, 0.2), disp=c(0.1, 0.2, 0.3), 
    force=c(0.1, 0.2, 0.3), disp=c(0.1, 0.2, 0.3))

new.df <- NULL
for (i in seq(2, ncol(df), 2)) {
    new.df <- rbind(new.df, data.frame(no=i/2, force=df[, i], disp=df[, i+1]))
}

library(ggplot2)
qplot(force, disp, group=no, data=new.df, col=factor(no), geom="line")

enter image description here

It's ugly, but works I guess.

Sign up to request clarification or add additional context in comments.

3 Comments

You're welcome, glad it works. Would you mind sharing what the plots look like in the end, if you can?
Done. I changed around the 'no' for the context of the plot.
Thanks! If you want to relabel the lines with more descriptive names, you could replace df$no with some more descriptive factor variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.