我在绘制折线图时遇到了一些问题,我希望我的绘图看起来更流畅。以下是我的代码:
Packages: "reshape2" , "forecast" , "zoo" , "xts" , "ggplot2"
input_data <- c(207, 165, 124, 89)
initial_value <- last(input_data)
growth_rate <- 0.05
#De-exponential growth model
exponential_growth_model <- lapply(1:6,function(time) {
r <- log(1 + growth_rate)
initial_value * exp(-r*time)
}
)
adj_result <- do.call(rbind, exponential_growth_model)
forecast_value <- c(input_data, adj_result)
year <- c(2003,2004,2005,2006,2007,2008,2009,2010,2011,2012)
mydata <- data.frame(year, forecast_value)
df <- melt(mydata, id = 'year', variable = 'series')
g <- ggplot(df, aes(year,value)) +
geom_line(aes(colour = series), size=1.5) +
geom_point(size = 3)
rect <- data.frame(xmin=2003, xmax=2006, ymin=-Inf, ymax=Inf)
print(g + geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), alpha=0.2, inherit.aes = FALSE))
我想让这个折线图看起来更平滑,就像图片中一样。谢谢!