r - Plotting multiple graphs in one page using loop -
i trying plot multiple graphs on same page. works fine when write code 1 one this:
old.par <- par(mfrow = c(3,4)) plot(na.omit(aus.yield), xlab = "date", ylab = "log_return", main = "aus.yield") plot(na.omit(bra.yield), xlab = "date", ylab = "log_return", main = "bra.yield") plot(na.omit(can.yield), xlab = "date", ylab = "log_return", main = "can.yield") plot(na.omit(chi.yield), xlab = "date", ylab = "log_return", main = "chi.yield") plot(na.omit(ger.yield), xlab = "date", ylab = "log_return", main = "ger.yield") plot(na.omit(jap.yield), xlab = "date", ylab = "log_return", main = "jap.yield") plot(na.omit(soa.yield), xlab = "date", ylab = "log_return", main = "soa.yield") plot(na.omit(swi.yield), xlab = "date", ylab = "log_return", main = "swi.yield") plot(na.omit(uk.yield), xlab = "date", ylab = "log_return", main = "uk.yield") plot(na.omit(us.yield), xlab = "date", ylab = "log_return", main = "us.yield")
but not work when try use for
loop e.g.:
## names(log_return[2:11]) give me aus.yield, bra.yield etc column names of dataframe log_return (i in names(log_return[2:11]){ plot(na.omit(i), xlab = "date", ylab = "log_return", main = "i") }
also, have generated 10 time series using:
for (i in 2:length(log_return)){ assign(names(log_return[i]),xts(log_return[i],log_return$date)) }
so got 10 time series names: aus.yield, bra.yield.... etc.
just wondering went wrong.... here error message:
error in plot.window(...) : need finite 'ylim' values
i thinking maybe got many nas in dataframe? have used na.omit
already.
here small section of datafram log_retrun;
date aus.yield bra.yield can.yield chi.yield ger.yield jap.yield 1 2008-01-01 na na na na na na 2 2008-01-02 na na na na na na 3 2008-01-03 -0.033047602 -0.01239795 0.003828977 -0.017857617 -0.031966192 na 4 2008-01-04 -0.003922215 0.00198792 -0.008443187 0.006734032 -0.006984895 na 5 2008-01-05 na na na na na na 6 2008-01-06 na na na na na na
the jap.yield may nas there values after several lines.
thank in advance , suggestion!! t_t
you don't need create xts
objects persistently. note modification of use of names()
function in for()
specification well:
for (i in names(log_return)[2:11]){ plot(na.omit(xts(log_return[[i]],log_return$date)), xlab = "date", ylab = "log_return", main = i) }
or, @spacedman mentions, can use get()
reference variables name here if want xts
objects in memory:
for (i in names(log_return)[2:11]){ plot(na.omit(get(i)), xlab = "date", ylab = "log_return", main = i) }
Comments
Post a Comment