简体   繁体   中英

Plotting legend and getting: Error in strwidth(legend, units = “user”, cex = cex, font = text.font) : plot.new has not been called yet

I've created multiple time series in rStudio. And I am attempting to plot it using ts.plot() . The plot works fine but adding a legend causes issues and gives :

Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet.

I have tried running all the code at once and adjusting the parameters.

ts.plot(nltnr.ts, peitnr.ts, nstnr.ts, nbtnr.ts, qbtnr.ts, ontnr.ts, mbtnr.ts, sktnr.ts, abtnr.ts, bctnr.ts, yktnr.ts, nttnr.ts, nvtnr.ts,
        gpars = list(
        ylab = "Amount in millions",
        col=1:12,
        legend("bottom", legend = 1:12)
        ))

ts.plot(nltnr.ts, peitnr.ts, nstnr.ts, nbtnr.ts, qbtnr.ts, ontnr.ts, mbtnr.ts, sktnr.ts, abtnr.ts, bctnr.ts, yktnr.ts, nttnr.ts, nvtnr.ts,
        gpars = list(
        ylab = "Amount in millions",
        col=1:12))
        legend("bottom", legend = 1:12)

Try running the legend() command after doing your plot, and not while running your plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM