Home > Software engineering >  Plotting in the same graph and getting the same axis in R
Plotting in the same graph and getting the same axis in R

Time:04-22

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))

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

DF <- data.frame(RMSFE_AR, RMSFE_RW, RMSFE_VAR, RMSFE_BVAR)
col <- c("red", "blue", "green", "dark blue")

plot(range(1:nrow(DF)), range(unlist(DF)), type = "n", xlab = "", ylab = "")
for(i in 1:4) lines(L[[i]], col = col[i], lty = 2)

or even easier using DF and col from above:

matplot(DF, lty = 2, type = "l", col = col)
  •  Tags:  
  • r
  • Related