绘制多个图时无效的xlim值

时间:2017-06-25 20:42:09

标签: r plot

我试图在一个窗口内绘制三个单独的图形。在每个图中,我正在绘制4个折线图。

我正在尝试为我的情节添加一个图例,当我添加它时,R会在plot.window(...)中抛出xlim错误

我已经注释了图例部分,代码运行正常,但很明显没有图例。

这个错误是否与使用par修改图形参数有关(mfrow = c(x,y))??

我的代码如下:

#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")

我希望首先让传奇工作在第一个子图上,然后我可以在其工作时更新其余的代码,所以请原谅代码的不完整性。

当我在第一个绘图函数中注释掉图例参数时,代码运行时没有错误,但是当包含它时会抛出xlim错误。

我怎样才能进一步解决这个错误,我不知道xlim被破坏了什么?它可能与顶部的par / mfrow部分有关吗?

使用最新的情节更新下面的代码

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 个答案:

答案 0 :(得分:1)

问题不是xlim,而是将legend传递到plot()函数中。这对我很有用:

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

修改

要在绘图区域的下方和外部显示常用图例,请使用:

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").

您可能需要调整oma/mar值或x和y