TheGoat
TheGoat

Reputation: 2867

Multiple plots in multiplot window using R

I have a data frame which has 12 variables in it, I wish to break the 12 variables into 3 separate plots using a single column of 3 rows.

My data frame is as follows:

> head(wideRawDF)
    Period.Start.Time DO0182U09A3 DO0182U09B3 DO0182U09C3 DO0182U21A1 DO0182U21A2 DO0182U21A3 DO0182U21B1 DO0182U21B2 DO0182U21B3
1 2017-01-20 16:30:00     -101.50     -103.37     -103.86     -104.78     -104.95     -105.33     -102.50      -99.43     -104.05
2 2017-01-20 16:45:00     -101.32     -102.75     -104.22     -104.51     -103.94     -105.29     -102.82     -101.99     -103.94
3 2017-01-20 17:00:00     -101.45     -103.30     -103.93     -104.70     -104.82     -105.13     -103.72     -103.95     -104.25
4 2017-01-20 17:15:00     -100.91      -95.92      -99.22     -103.83     -104.72     -105.19     -103.57     -101.36     -104.09
5 2017-01-20 17:30:00     -100.91     -103.04     -104.09     -102.15     -104.91     -105.18     -103.88     -104.09     -103.96
6 2017-01-20 17:45:00     -100.97     -103.67     -104.12     -105.07     -104.23      -97.48     -103.92     -103.89     -104.01
  DO0182U21C1 DO0182U21C2 DO0182U21C3
1     -104.51     -104.42     -105.17
2     -104.74     -104.65     -105.25
3     -105.02     -105.04     -105.32
4     -103.90     -102.95     -105.16
5     -104.75     -105.07     -105.23
6     -105.08     -105.14     -104.89

I have managed to plot the variables DO0182U09A3, DO0182U09B3 and DO0182U09C3 in three separate plots. I would like to add the "U21A1", "U21A2" and "U21A3" variables to the "U09A3" plot, similarly the "U21B1", "U21B2" and "U21B3" to "U09B3" plots and same again for the "C" plots.

I am a complete novice at R and have hacked a few lines together from here as to how to create multiple plots in a single window but am unsure as how to plot multiple variables in multiple windows, can anyone give a dig out?

If I could also colour all the "U09" lines as a single colour, e.g red, and the U21A1,U21B1 and U21C1 as green and similarly for U21A2,B2 and C2 as blue and again for U21A3,B3 and C3 as purple that would be fantastic. b b    ×

#3 plots representing each of the sectors and technologies for DO0182
op <- par(no.readonly = TRUE)
par(oma=c(3,3,0,0),mar=c(3,3,2,2),mfrow =c(3,1))

plot(wideRawDF_NA$Period.Start.Time,wideRawDF$DO0182U09A3,ylab="",xlab="",type="l",col = "red", lwd = 1,  ylim = c(-107, -80),title(main = "Test"), sub = "subtitle")
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A3 , col = "mediumpurple4")

plot(wideRawDF_NA$Period.Start.Time,wideRawDF$DO0182U09B3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -70))
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B3 , col = "mediumpurple4")

plot(wideRawDF_NA$Period.Start.Time,wideRawDF$DO0182U09C3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -80))
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C3 , col = "mediumpurple4")

cols <- rainbow(12)
mtext(text="Time",side=1,line=0,outer=TRUE)
mtext(text="Received Total Wideband Power (dBm)",side=2,line=0,outer=TRUE)

legend(x=1,y=1.7,legend=wideRawDF_NA[,2:13],col=unique(cols),pch=16,bty="n",xpd=NA)

par(old.par)

Upvotes: 0

Views: 175

Answers (1)

Edgar Santos
Edgar Santos

Reputation: 3514

You are almost there. Just need to add the lines to your plot using lines() and select the colors. See ?lines and colors(). Also, you might want to use main() to add a main title to the individual plots. How about:

par(oma=c(3,3,0,0),mar=c(3,3,2,2),mfrow =c(3,1))

plot(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U09A3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-105, -95))
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21A3 , col = "mediumpurple4")


plot(wideRawDF_NA$Period.Start.Time,wideRawDF$DO0182U09B3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-105, -95))
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21B3 , col = "mediumpurple4")

plot(wideRawDF_NA$Period.Start.Time,wideRawDF$DO0182U09C3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-105, -95))
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C1 , col = 3)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C2 , col = 4)
lines(wideRawDF_NA$Period.Start.Time,wideRawDF_NA$DO0182U21C3 , col = "mediumpurple4")

enter image description here

Upvotes: 1

Related Questions