OGC
OGC

Reputation: 274

Formatting a graph with ts.plot in R

In R, I am trying to graph the two time series together more nicely. For the y-axis, I am trying to include the "Percentage" label but for some reason it isn't showing up on the graph. AlsoI am trying to get rid of the tick marks and also want to get ride of the box, but keep the lines for the x-and-y-axes. Any ideas would be helpful. I have my codes below:

# Plot the two series with ts.plot
par(mfrow = c(1,1))
ts.plot(IOER, deposit_rate, col = c("cornflowerblue", "firebrick1"), xlab = "", ylab = "Percentage", lwd = 2, lty=c(1,2))
legend("topleft", bty="n", lty=c(1,2), col=c("cornflowerblue","firebrick1"),
       legend=c("Interest on Excess Reserves", "Average Interest Rate on Transactions"))

enter image description here

In the interest of reproducibility, I have created this example dataset (see below). Note that this does not represent the actual dataset.

data_example <- data.frame(date = c("2008-10-01",    # Create example data
                            "2009-01-01",
                            "2009-04-01",
                            "2009-07-01",
                            "2009-10-01",
                            "2009-10-01",
                            "2009-10-01",
                            "2010-01-01",
                            "2010-04-01",
                            "2010-07-01",
                            "2010-10-01"),
                            ioer_ex = c(0.79, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25),
                            avg_intrate_trans_ex = c(0.01, 0.05, 0.03, 0.02, 0.01, 0.03, 0.04, 0.07, 0.08, 0.06, 0.02))

# Declare our time series variables 
IOER <- ts(data_example$ioer, start = c(2008,4), frequency = 4)
deposit_rate <- ts(data_example$avg_intrate_trans, start = c(2008,4), frequency = 4)

Also maybe can include a subtitle or a title

EDIT: I able to the add the title so just need some help to get rid of the ticks and the box but keeping the x and y axes lines

# Plot the two series with ts.plot
par(mfrow = c(1,1))
ts.plot(IOER, deposit_rate, col = c("cornflowerblue", "firebrick1"), xlab = "", ylab = "", lwd = 2, lty=c(1,2))
legend("topleft", bty="n", lty=c(1,2), col=c("cornflowerblue","firebrick1"),
       legend=c("Interest on Excess Reserves", "Average Interest Rate on Transactions"))
title(main = "Percent, Oct. 2008-Oct. 2019", adj = 0, line = 0.25, font.main = 1)

Updated graph:

enter image description here

To remove the ticks, I have tried xaxt = 'n' (and yaxt = 'n' for y axis) inside ts.plot but it didn't work. Same goes for removing the box: bty = "n" inside ts.plot isn't working in the way it works in plot.

Upvotes: 1

Views: 1104

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269586

Remove the par line and add the gpars= argument shown in this ts.plot call. bty="n" removes the box and tcl=0 removes the ticks. See ?par for more info.

ts.plot(IOER, deposit_rate, col = c("cornflowerblue", "firebrick1"), 
  xlab = "", ylab = "", lwd = 2, lty = 1:2, gpars = list(bty = "n", tcl = 0))

Upvotes: 1

Related Questions