0

I have the following dataset:

Time               Served        MAC             BLER
07:16:18.341       7561.60       6721.60             8.33
07:16:18.641       10321.44          8198.24             16.47

I have omitted the other samples for brevity.

I would like to plot in the x-axis Time, and the Y axis the three other variables i.e. Served, MAC and BLER on the same graph in ggplot2. How can I do this? many thanks

agstudy
  • 119,832
  • 17
  • 199
  • 261
Paul Lugano
  • 75
  • 1
  • 2
  • 5

1 Answers1

2

you need data in melted form.

require(reshape2)
df.m<-melt(df,id.var="Time")

then

ggplot(df.m, aes(x=Time, y=value, color=variable))+geom_line()

but I am afraid about the scaling of y axis

Ananta
  • 3,671
  • 3
  • 22
  • 26
  • Is there a way to plot the BLER on a Secondary Y-axis? That way the scaling of the Y axis can be avoided? – Paul Lugano Oct 30 '13 at 00:45
  • not in ggplot2, see this thread, a good discussion and rant , http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-plot-with-2-y-axes-one-y-axis-on-the-left-and-another – Ananta Oct 30 '13 at 00:47
  • When I try the following code: df.m<-melt(set,var="Time"); ggplot(df.m, aes(y=value, colour=variable))+geom_line()code; I get the following error: Error in eval(expr, envir, enclos) : object 'variable' not found – Paul Lugano Oct 30 '13 at 00:54
  • did the melt work? what are the variables in `df.m` now? try with `head`, or `colnames` – Ananta Oct 30 '13 at 01:00
  • see the edit, I forgot x-axis earlier. – Ananta Oct 30 '13 at 01:06
  • Thanks Ananta. The logic of the code you wrote works basically works. I will move BLER to the secondary axis to get a good plot. Thanks. Here is the code I used: df.m2<-melt(df.m,id.var="Time"); ggplot(df.m2, aes(x=Time, value, colour=variable))+geom_line(); – Paul Lugano Oct 30 '13 at 01:09