2

Hello everyone i am having a problem with grouping my data and plotting it further in ggplot(). My data consists of few columns, which 4 first ones (all together) correspond to the "sample ID", the other two which is Zeit(Time in seconds) and Temp.

I need to plot Time vs Temp plot for each of the sample, however to group it by their ID is very chalenging and at the moment i cannot figure it out.

Sample data:

> dput(sampledata)
structure(list(a = c(703210L, 703210L, 703210L, 703210L, 703210L, 
                        703210L, 703210L, 703210L, 703210L, 703210L, 703210L, 703210L, 
                        703210L, 703210L, 703210L, 703210L, 703210L, 703210L, 703210L, 
                        703210L), b = c(3988L, 3988L, 3988L, 3988L, 3988L, 3988L, 
                                            3988L, 3988L, 3988L, 3988L, 3988L, 3988L, 3988L, 3988L, 3988L, 
                                            3988L, 3988L, 3988L, 3988L, 3988L), c = c(1L, 1L, 1L, 1L, 
                                                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L
                                            ), d = c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                          4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L),  Zeit = c(0L, 240L, 300L, 420L, 540L, 546L, 
                                                                                                                                           0L, 180L, 300L, 360L, 540L, 546L, 0L, 180L, 300L, 360L, 540L, 
                                                                                                                                           545L, 0L, 120L), Temp = c(913L, 675L, 570L, 514L, 515L, 355L, 
                                                                                                                                                                     867L, 687L, 575L, 543L, 518L, 437L, 874L, 690L, 577L, 562L, 529L, 
                                                                                                                                                                     455L, 856L, 721L)), .Names = c("a", "b", "c", "d", 
                                                                                                                                                                                                    "Zeit", "Temp"), row.names = 2317:2336, class = "data.frame")

The same data, but in format which i can explain exactly what i meant by 4 columns defining the sample ID:

          a    b c d Zeit Temp
2317 703210 3988 1 1    0  913
2318 703210 3988 1 1  240  675
2319 703210 3988 1 1  300  570
2320 703210 3988 1 1  420  514
2321 703210 3988 1 1  540  515
2322 703210 3988 1 1  546  355 
2323 703210 3988 1 3    0  867#here starts the new sample (d=3)
2324 703210 3988 1 3  180  687
2325 703210 3988 1 3  300  575
2326 703210 3988 1 3  360  543
2327 703210 3988 1 3  540  518
2328 703210 3988 1 3  546  437
2329 703210 3988 2 4    0  874#here starts the new sample (c=2)
2330 703210 3988 2 4  180  690
2331 703210 3988 2 4  300  577
2332 703210 3988 2 4  360  562
2333 703210 3988 2 4  540  529
2334 703210 3988 2 4  545  455
2335 703210 3988 3 5    0  856#here starts the new sample (c=3 & d=5)
2336 703210 3988 3 5  120  721

and so on...It as well happens that a & b changes, so together all those 4 columns define the sample ID

I tried to plot it in ggplot() easily:

ggplot(sampledata, aes(x=Zeit, y=Temp)) + geom_line(size=1.5) #however it was total chaos

Than i tried to use interaction():

ggplot(sampledata, aes(x=Zeit, y=Temp, group=interaction(a,b,c,d))) + geom_line(size=1.5)

as well i got the plot which was very chaotic...

Anyone has an idea how i can plot it?

Thanks for any advice!

3
  • may be you can create additionall colunm which concate your 4 colunm into one ID colunm? (paste) Commented Feb 26, 2016 at 7:03
  • hey, i have just tried that, and still is chaotic. the problem is when i group it by a new column in ggplot, the time gets mixed up, instead of each plot curve starting from 0 (as 0 seconds), all get together mixed and make no sense Commented Feb 26, 2016 at 7:11
  • Try to show pictures what you get and what want... Commented Feb 26, 2016 at 7:19

1 Answer 1

2

You can create a id variable in your data set and then give color as id in plot. Below is the code:

library(data.table)
library(ggplot2)
sampledata <- data.table(sampledata)
sampledata[, id:=paste(a,b,c,d, sep="_")]

ggplot(sampledata, aes(x=Zeit, y=Temp, color=id)) + geom_line(size=1.5)

With color argument, you can see different samples clearly. Hope this helps. :)

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

Comments

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.