1
votes

I am trying to plot three separate graphs within a single window. Within each graph I am plotting 4 line graphs.

I am trying to add a legend to my plots and when I add this in, R throws an xlim error in plot.window(...)

I have commented out the legend section and the code runs OK but obviously without the legend.

Would this error have something to do with modifying the graphical parameters using par(mfrow=c(x,y))??

My code looks as follows:

#Three 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$Period.Start.Time,wideRawDF$DO0182U09A3,
     ylab="",
     xlab="",
     type="l",
     col = "red",
     lwd = 1,
     ylim = c(-107, -80),
     main = "Sector A",
     legend(x="topleft",
            ncol = 4,
            legend = c("F0 - U900MHz",
                       "F1 - U2100MHz",
                       "F2 - U2100MHz",
                       "F3 - U2100MHz"),
            fill = c("red","cyan","magenta","black"),
            title = "Frequency Bands"))
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A3 , col = "black")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09B3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -70), main = "Sector B")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B3 , col = "black")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09C3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -80), main = "Sector C")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C3 , col = "black")

I was hoping to get the legend working on the first subplot first and then I can update the rest of the code when it works so please excuse the incompleteness of code.

When I comment out the legend argument in the very first plot function the code runs without error as mention but when included it throws an xlim error.

How can I go about troubleshooting this error a bit further, I don't know what xlim is being breached? Could it be related to the par/mfrow section at the top?

Updated code below with most recent plot

op <- par(no.readonly = TRUE)
par(oma=c(2,2,0,13),mar=c(3,3,2,2),mfrow =c(3,1))
box("outer", lty="solid", col="green")
box("inner", lty="dotted", col="green")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09A3,
     ylab="",
     xlab="",
     type="l",
     col = "#e41a1c",
     lwd = 1,
     ylim = c(-107, -80),
     main = "Sector A")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A1 , col = "#377eb8")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A2 , col = "#4daf4a")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A3 , col = "#984ea3")

par(xpd=NA)
legend(x=-3,
       y= 7,
       inset = 0,
       ncol = 1,
       legend = c("F0 - U900MHz",
                  "F1 - U2100MHz",
                  "F2 - U2100MHz",
                  "F3 - U2100MHz"),
       fill = c("#e41a1c","#377eb8","#4daf4a","#984ea3"),
       title = "Frequency Bands")
box("outer", lty="solid", col="green")
box("inner", lty="dotted", col="green")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09B3,
     ylab="",
     xlab="",
     type="l",
     col = "#e41a1c", 
     lwd = 1, 
     ylim = c(-107, -70), 
     main = "Sector B")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B1 , col = "#377eb8")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B2 , col = "#4daf4a")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B3 , col = "#984ea3")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09C3,
     ylab="",
     xlab="",
     type="l",
     col = "#e41a1c",
     lwd = 1, 
     ylim = c(-107, -80), 
     main = "Sector C")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C1 , col = "#377eb8")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C2 , col = "#4daf4a")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C3 , col = "#984ea3")


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

enter image description here

1

1 Answers

1
votes

The problem is not xlim but passing the legend inside the plot() function. This works well for me:

op <- par(no.readonly = TRUE)
par(oma=c(3,3,0,0),mar=c(3,3,2,2),mfrow =c(3,1))

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09A3,
     ylab="",
     xlab="",
     type="l",
     col = "red",
     lwd = 1,
     ylim = c(-107, -80),
     main = "Sector A")

legend(x="topleft",
       ncol = 4,
       legend = c("F0 - U900MHz",
                  "F1 - U2100MHz",
                  "F2 - U2100MHz",
                  "F3 - U2100MHz"),
       fill = c("red","cyan","magenta","black"),
       title = "Frequency Bands")

lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21A3 , col = "black")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09B3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -70), main = "Sector B")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21B3 , col = "black")

plot(wideRawDF$Period.Start.Time,wideRawDF$DO0182U09C3,ylab="",xlab="",type="l",col = "red", lwd = 1, ylim = c(-107, -80), main = "Sector C")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C1 , col = "cyan")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C2 , col = "magenta")
lines(wideRawDF$Period.Start.Time,wideRawDF$DO0182U21C3 , col = "black")

enter image description here

Edit:

For displaying a common legend below and outside of the plotting area use:

par(xpd=T)
legend(ymd_hms("2017-01-20 16:30:00"), -112,
       ncol = 4,
       legend = c("F0 - U900MHz",
                  "F1 - U2100MHz",
                  "F2 - U2100MHz",
                  "F3 - U2100MHz"),
       fill = c("red","cyan","magenta","black"),
       title = "Frequency Bands").

You might need to adjust your oma/mar values or the x and y