0

I am trying to plot multiple graphs on the same page. It works fine when I write the code one by one like 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 it does not work when I try to use a for loop e.g.:

## names(log_return[2:11]) will give me all those AUS.Yield, BRA.Yield etc as they are the column names of dataframe log_return

for (i in names(log_return[2:11]){
    plot(na.omit(i), xlab = "Date", ylab = "log_return", main = "i")
}

Also, I have generated 10 time series by using:

for (i in 2:length(log_return)){
  assign(names(log_return[i]),xts(log_return[i],log_return$Date))
}

so I got 10 time series that names: AUS.Yield, BRA.Yield.... etc.

Just wondering where went wrong.... Here is the error message:

 Error in plot.window(...) : need finite 'ylim' values 

I am thinking maybe I got so many NAs in my dataframe? But I have used the na.omit already.

Here is a small section of the 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 look all NAs but there are values after a several lines.

Thank you in advance for any help and suggestion!! T_T

2 Answers 2

1

You don't need to create the xts objects persistently. Note the modification of the use of your names() function in the for() specification as 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, as @SpacedMan mentions, you can use get() to reference the variables by name here as well if you do want the xts objects in memory:

for (i in names(log_return)[2:11]){
    plot(na.omit(get(i)), xlab = "Date", ylab = "log_return", main = i)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! The problem is solved now. I think I need more readings on the basics otherwise I will mess up again on those strings and names things.
0

The name of a thing is a different thing from the thing itself.

The string "foo" and the object foo are different things. If you plot("foo") it tries to plot the string "foo". If you plot(foo) then you plot the object called foo.

So, what are you doing:

for (i in names(log_return[2:11]){
    plot(na.omit(i), xlab = "Date", ylab = "log_return", main = "i")
}

names(something) returns the names as strings. So you are doing plot("AUS.yield"), which is plotting the string and not the thing.

Solutions: best is to NOT make things with names using assign. Instead create a list with named objects. Then you can do something like name="AUS.yield";data[[name]] to get the data from a name in a string.

worst solution is to use get(name), which tries to get the thing with the given name (in your case something like plot(get(i)) should do it. This is the "worst" solution because its a fiddle to undo the mistake you originally made, of creating lots of objects by name when you should have structured them in a list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.