2

I have a data frame which contains a column of time series data and 9 other variables with signal strength values for each time, see below:

    head(RTWP_Columns)
    Period.Start.Time DU0362U09A3 DU0362U09B3 DU0362U09C3 DU0362U21A1 DU0362U21A2 DU0362U21B1 DU0362U21B2 DU0362U21C1
1 01.16.2017 00:00:00     -104.54     -106.43     -104.40     -104.48     -103.04     -104.50     -103.58     -104.10
2 01.16.2017 00:15:00     -104.98     -106.49     -104.48     -104.47     -103.40     -104.50     -103.81     -104.22
3 01.16.2017 00:30:00     -105.34     -106.45     -104.50     -104.50     -103.23     -104.50     -104.01     -104.26
4 01.16.2017 00:45:00     -105.30     -106.48     -104.48     -104.50     -103.38     -104.41     -104.10     -104.32
5 01.16.2017 01:00:00     -104.99     -106.49     -104.50     -104.50     -103.44     -104.50     -104.36     -104.24
6 01.16.2017 01:15:00     -105.33     -106.49     -104.49     -104.50     -103.82     -104.50     -104.39     -104.39
  DU0362U21C2
1      -97.48
2      -99.18
3     -101.04
4     -101.76
5     -101.75
6     -101.97

I wish to plot each variable as a line in a single graph and colour them according to their variable name.

Plotting using the run of the mill plot() and qplot() is fine but when it comes to aesthetics I have great difficulty as I am a novice R user.

How can I plot my data frame such that time is on the X axis and my columns are shown as separate lines? Since my time is of type factor is this the cause of my grief? I have created a separate posixct for my time but I can't plot this either in ggplot.

Below is the structure of my dataframe:

   str(RTWP_Columns)
'data.frame':   672 obs. of  10 variables:
 $ Period.Start.Time: Factor w/ 672 levels "01.16.2017 00:00:00",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ DU0362U09A3      : num  -105 -105 -105 -105 -105 ...
 $ DU0362U09B3      : num  -106 -106 -106 -106 -106 ...
 $ DU0362U09C3      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21A1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21A2      : num  -103 -103 -103 -103 -103 ...
 $ DU0362U21B1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21B2      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21C1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21C2      : num  -97.5 -99.2 -101 -101.8 -101.8 ...

I've used the following plot function but I can't translate it to a ggplot equivalent:

plot(times,RTWP_Columns$DU0362U09A3,
       type = "l",
       lwd=2,col="red",
       xlab = "Time",
       ylab = "RTWP (dBm)",
       main = "RTWP Plot for DU0362")
lines(times,RTWP_Columns$DU0362U09B3, col="green",lwd=2)
lines(times,RTWP_Columns$DU0362U09C3, col="blue",lwd=2)
lines(times,RTWP_Columns$DU0362U21A1, col="orange",lwd=2)
lines(times,RTWP_Columns$DU0362U21A2, col="purple",lwd=2)
lines(times,RTWP_Columns$DU0362U21B1, col="black",lwd=2)
lines(times,RTWP_Columns$DU0362U21B2, col="cyan",lwd=2)
lines(times,RTWP_Columns$DU0362U21C1, col="yellow",lwd=2)
lines(times,RTWP_Columns$DU0362U21C2, col="pink",lwd=2)

where times is my posixct equivalent of the period.start.time in RTWP_Columns.

If you could provide a pointer in the right direction I would be very grateful.

TheGoat
  • 2,587
  • 3
  • 25
  • 58
  • 1
    have you looked into melt(df) way of plotting? Are you thinking about spaghetti plot ? something like this http://www.ats.ucla.edu/stat/r/faq/spagpl2.gif – Mandar Jan 29 '17 at 22:20
  • @Mandar thanks for the pointer, melt worked a treat. Thanks for taking the time for replying, much appreciated. – TheGoat Jan 30 '17 at 07:59

1 Answers1

6

Something like this

library(ggplot2)
library(reshape2)
meltdf <- melt(df, id="Time")
ggplot(meltdf, aes(x=Time, y=value, colour=variable, group=variable)) + geom_line()

Check this for details Plotting multiple time-series in ggplot

ah bon
  • 9,293
  • 12
  • 65
  • 148
Mandar
  • 1,659
  • 1
  • 10
  • 14