for loop multiple line plots

For different interest_rates , I am trying to plot how 100 dollars will grow in 10 years.

I have manged to do it in Python. Using similar approch, I have tried to do it in R. Unfortunately , line plots appear three separate graphs. Below are my attempts.

Python

import numpy as np
import matplotlib.pyplot as plt
interest_rates = [.12, .15, .2]
years = 10
amount = np.empty(years + 1)
for i in interest_rates:
    amount[0] = 100
    for year in range(years):
        amount[year + 1] = amount[year]*(1 + i)
    plt.plot(amount, label = f'$\\alpha = {i}$')
plt.legend()
plt.show()

plot-python

R

interest_rates <- c(.12, .15, .2)
years <- 10
amount <- vector("double", years + 1)

for (i in interest_rates){
  amount[1] <- 100
  for (year in seq(years)){
    amount[year + 1] = amount[year] * (1 + i)
  }
  plot(amount, type = "line")
}
#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

Created on 2021-08-14 by the reprex package (v2.0.0)

@Yarnabrina thanks for the tip!

I have partially solved the problem. Here are some issues:

  • I have manually set the ylim = 700 . How do I set it to max(amount)?
  • How do I get colored lines and legend (like the Python plot above)?
interest_rates <- c(.12, .15, .2)
years <- 10
amount <- vector("double", years + 1)
# Empty plot
plot.new()
plot(1, type="n", xlab="Year", ylab="Amount", xlim=c(0, years), ylim = c(100, 700))

for (i in interest_rates){
  amount[1] <- 100
  for (year in seq(years)){
    amount[year + 1] = amount[year] * (1 + i)
  }
  lines(x =0:10, y = amount, type = "l")
}

Created on 2021-08-14 by the reprex package (v2.0.0)

Here is one way to do those. But I won't recommend doing this way.

Relevant Code
# Setup
interest_rates <- c(0.12, 0.15, 0.2)
years <- 10
amount <- vector("double", years + 1)

# Limit calculation
y_min <- 100
y_max <- 100 * (1 + max(interest_rates))^years
y_range <- c(y_min - 50, y_max + 50)

# Empty plot
plot(NULL, xlab = "Year", ylab = "Amount", xlim = c(0, years), ylim = y_range)

# Comparative lines
for (i in seq_along(interest_rates)) {
  amount[1] <- 100
  for (year in seq(years)) {
    amount[year + 1] <- amount[year] * (1 + interest_rates[i])
  }
  lines(x = 0:10, y = amount, type = "l", lty = 1, col = i + 1)
}

# Legends
legend("topleft", legend = paste0("alpha = ", interest_rates), lty = 1, col = 1 + seq_along(interest_rates))

Here is another way using base graphics only.

# Setup
interest_rates <- c(0.12, 0.15, 0.2)
no_years <- 10

# Calculation
initial_amount <- 100
time_frame <- 0:no_years
cumulative_amounts <- vapply(interest_rates, \(r) {
  initial_amount * (1 + r)^time_frame
}, numeric(length(time_frame)))

# Plot
matplot(time_frame, cumulative_amounts, type = "l", lty = 1, col = 1 + seq_along(interest_rates), xlab = "Year", ylab = "Amount", xlim = c(0, no_years), ylim = range(cumulative_amounts) + c(-50, 50))
legend("topleft", legend = interest_rates, lty = 1, col = 1 + seq_along(interest_rates), title = expression(alpha))

Hope this helps.

1 Like

@Yarnabrina Thanks a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.