【发布时间】:2019-10-14 06:05:52
【问题描述】:
如何根据公式中不同的参数值绘制多条不同的线兼点曲线?
我想绘制一个简单的公式,比如复利。 x 轴应该有年份,y 轴应该有最终金额。应该有几条曲线,一条对应于同一组轴上的每个利率。我只得到一列包含所有值,而不是几列,一个用于每个速率值。
r <- c(0,.05,.08,.1,.15) # Interest rates
C <- 100 # Initial amount
t <- seq(0, 20, by = 1) # Say, 20 years investment
fv <- C*(1+r)^t
df <- data.frame(cbind(t,fv)) # creates the data frame but with only 2 columns.
ggplot(df)+ # will obviously not plot several curves
geom_point(aes(x = t, y = FV), size = 3)+
geom_line() # I need a line for each r value
xlab("Number of years")+
ylab(paste("Future value of Rupees",C))
使用上述矢量化方法是否有效?或者我需要一个 for 循环吗?
【问题讨论】: