Can someone explain how I can create subplots. I have 11 plots I want to have in the same window, and not separate.
The style I am looking for is as follows:
Please, find my code below:
# Plot risk free rate
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(-0.08, max(rt)*5)), main = "Simulated Path for Short-Rate", xlab = "Years", ylab = "Short-Rate")
} else {
lines(rt, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(-0.08, max(rt)*5))
}
# Plot Government bonds, share class 1
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(GovBond), max(GovBond))), main = "Simulated Path for Government Bonds", xlab = "Years", ylab = "Government Bonds")
} else lines(GovBond, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(GovBond), max(GovBond)*5))
# Plot for Investment grade bonds, share class 2
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(InvBond), max(InvBond))), main = "Simulated Path for Investment Grade Bonds", xlab = "Years", ylab = "Investment Grade Bonds")
} else lines(InvBond, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(InvBond), max(InvBond)))
# Plot for High Yield bonds, share class 3
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(HYBond), max(HYBond)*5)), main = "Simulated Path for High Yield Bonds", xlab = "Years", ylab = "High Yield Bonds")
} else lines(HYBond, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(HYBond), max(HYBond)))
# Plot for Emerging market government bonds, share class 4
if(sim_i == 1) {
plot(tid, EMGovBond, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(EMGovBond), max(EMGovBond))), main = "Simulated Path for Emerging Markets Government Bonds", xlab = "Years", ylab = "Emerging Markets Bonds")
} else lines(EMGovBond, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(EMGovBond), max(EMGovBond)))
# Plot for Global equity, share class 5
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(GStock), max(GStock))), main = "Simulated Path for Global Equity", xlab = "Years", ylab = "Global Equity")
} else lines(GStock, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(GStock), max(GStock)))
# Plot for Emerging markets equity, share class 6
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(EMStock), max(EMStock))), main = "Simulated Path for Emerging Earkets Equity", xlab = "Years", ylab = "Emerging Markets Equity")
} else lines(EMStock, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(EMStock), max(EMStock)))
# Plot for Private equity, share class 7
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(Pe), max(Pe))), main = "Simulated Path for Private Equity", xlab = "Years", ylab = "Private Equity")
} else lines(Pe, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(Pe), max(Pe)))
# Plot for Infrastructure, share class 8
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(min(InSt), max(InSt))), main = "Simulated Path for Infrastructure", xlab = "Years", ylab = "Infrastructure")
} else lines(InSt, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(InSt), max(InSt)))
# Plot for Real estate, share class 9
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(0, 250)), main = "Simulated Path for Real Estate", xlab = "Years", ylab = "Real Estate")
} else lines(ReEst, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(ReEst), max(ReEst)))
# Plot for Hedge funds, share class 10
if(sim_i == 1) {
plot(tid, type = "l", lty=2, xlim=(c(2,Nyear)), ylim=(c(0, 200)), main = "Simulated path for Hedge Funds", xlab = "Years", ylab = "Hedge Funds")
} else lines(HF, col = sim_i, type = 'l', lty = 2, xlim=(c(2,Nyear)), ylim=c(min(HF), max(HF)))
CodePudding user response:
It would probably be best to use ggplot
here, but assuming you don't want to or know how to do that, you lay out multiple plots using par(mfrow = c(2, 6))
, which will give you two rows of 6 plots (enough for 11 separate plots). You will need to ensure your plotting window is big enough to accommodate the plots, otherwise you will get a "margin too large" error.
Since we don't have your data, we can't replicate your plots, but the following example demonstrates the idea using random numbers:
par(mfrow = c(2, 6))
for(i in 1:11) plot(x = 1:10, y = cumsum(runif(10)), type = "l")