Susie Ringblom
Susie Ringblom

Reputation: 1

Plotting in the same graph and getting the same axis in R

I have been trying to plot my results in a graph. However, when watching the result I see that each have its own y-axis that does not correspond to the others. How could I possibly fix my problem? You can see that the y-axis does not match with the result

RMSFE_AR <- c(0.380231, 0.3965452, 0.3408504, 0.398722)
RMSFE_RW <- c(0.492315, 0.4034821, 0.3671059, 0.3884792)
RMSFE_VAR <- c(0.429498, 0.4180072, 0.3546557, 0.4059629)
RMSFE_BVAR <- c(0.34, 0.3588872, 0.3510698, 0.3411622)

par(mfrow = c(1,1))
plot(RMSFE_AR, type = "l",
     ylab = "", xlab = "", col = "red", lty = 2, xaxt="n", ylim=c(0.32,0.5))
axis(1, at = 1:4, labels = c("1", "3",  "6", "12"))
par(new = TRUE)
plot(RMSFE_RW, type = "l",
     ylab = "", xlab = "", col = "blue", lty = 2, axes=FALSE)
par(new = TRUE)
plot(RMSFE_VAR, type = "l",
     ylab = "", xlab = "", col = "green", lty = 2, axes=FALSE)
par(new = TRUE)
plot(RMSFE_BVAR, type = "l",
     ylab = "", xlab = "", col = "dark blue", lty = 2, axes=FALSE)
mtext("", side = 4, line = 3)
legend(par('usr')[1], par('usr')[3], bty='n', xpd=NA,cex = 0.7, 
       c("RMSFE AR(1)", "RMSFE RW", "RMSFE VAR", "RMSFE BVAR"), 
       col = c("red", "blue", "green", "dark blue"), lty = c(2, 2, 2, 2))

Upvotes: 0

Views: 105

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269481

Create a data.frame, DF, from the data and col color vector and then use plot with type = "n" to set the appropriate ranges and then actually draw the lines using lines.

# inputs
DF <- data.frame("RMSFE AR(1)" = RMSFE_AR, 
                 "RMSFE RW" = RMSFE_RW, 
                 "RMSFE VAR" = RMSFE_VAR, 
                 "RMSFE BVAR" = RMSFE_BVAR, 
                   row.names = c(1, 3, 6, 12),
                   check.names = FALSE)
col <- c("red", "blue", "green", "dark blue")

x <- 1:nrow(DF)
plot(range(x), range(DF), type = "n", xlab = "", ylab = "", xaxt = "n")
axis(1, at = x, labels = rownames(DF))
for(i in 1:ncol(DF)) lines(DF[[i]], col = col[i], lty = 2)
legend("topright", legend = names(DF), bty = 'n', xpd = NA, cex = 0.7, 
   col = col, lty = 2)

or even easier using DF and col from above:

x <- 1:nrow(DF)   
matplot(DF, lty = 2, type = "l", col = col, ylab = "", xaxt = "n")
axis(1, at = x, labels = rownames(DF))
legend("topright", legend = names(DF), bty = 'n', xpd = NA, cex = 0.7, 
   col = col, lty = 2)

Upvotes: 1

Zoe
Zoe

Reputation: 1000

Sorry, but it has to be done: You could use ggplot2.

library(ggplot2)

# reshape data so ggplot likes it
df <- data.frame(
  var = rep(c("RMSFE_AR", "RMSFE_RW", "RMSFE_VAR", "RMSFE_BVAR"), each = 4),
  x = rep(c(1,3,6,12), 4),
  y = c(RMSFE_AR, RMSFE_RW, RMSFE_VAR, RMSFE_BVAR))

# make a plot
ggplot(df, aes(x = x, y = y, col = var)) +
  geom_line(linetype = "longdash", size = 1) + # add line
  scale_color_discrete(labels = c("RMSFE AR(1)", "RMSFE RW", "RMSFE VAR", "RMSFE BVAR")) + # rename legend items
  labs(col = "", x = "", y = "") + # set legend and axis titles
  theme_light() # prettier

enter image description here

Upvotes: 0

Quinten
Quinten

Reputation: 41235

You can use the following code:

plot(RMSFE_AR, type = "l",
     ylab = "", xlab = "", col = "red", lty = 2, xaxt="n", ylim=c(0.32,0.5))
axis(1, at = 1:4, labels = c("1", "3",  "6", "12"))
lines(RMSFE_RW, type = "l", col = "blue", lty = 2, axes=FALSE)
lines(RMSFE_VAR, type = "l", col = "green", lty = 2, axes=FALSE)
lines(RMSFE_BVAR, type = "l", col = "dark blue", lty = 2, axes=FALSE)
mtext("", side = 4, line = 3)
legend("topright", bty='n', xpd=NA,cex = 0.7, 
       c("RMSFE AR(1)", "RMSFE RW", "RMSFE VAR", "RMSFE BVAR"), 
       col = c("red", "blue", "green", "dark blue"), lty = c(2, 2, 2, 2)) 

Output:

enter image description here

Upvotes: 0

Related Questions