r - 具有 10,000 个组的 geom_line
问题描述
我正在对一个变量进行 10,000 次模拟,我想将每个模拟绘制在一个图中。我无法用 geom_line 做一个很好的情节,因为看起来我有很多组。
这是一个可重现的示例和我正在制作的情节。
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggfan)
library(gridExtra)
library(stringr)
library(scales)
#Create a dataframe
month <- 1:120
price_a <- 5000
demand <- 10
data <- data.frame(month, price_a, demand)
#Create 100 simulations to project price_a and demand for the future
simulations <- 10000
intervalo <- seq_len(120)
set.seed(96)
lista_meses <- lapply(setNames(intervalo, paste0("data", intervalo)), function(i) {
cbind(
data[rep(i, simulations),],
growth_pricea = as.numeric(runif(simulations, min = -0.02, max = 0.05)),
growth_demand = as.numeric(runif(simulations, min = -0.03, max = 0.03)),
revenue = demand*price_a
)
})
#Calculate the growth of each variable and revenue
for (i in 2:length(lista_meses)){
lista_meses[[i]][["price_a"]] <- lista_meses[[i-1]][["price_a"]]*(1+lista_meses[[i]][["growth_pricea"]])
lista_meses[[i]][["demand"]] <- lista_meses[[i-1]][["demand"]]*(1+lista_meses[[i]][["growth_demand"]])
lista_meses[[i]][["revenue"]] <- lista_meses[[i]][["price_a"]]*lista_meses[[i]][["demand"]]
}
#Extract revenue columns from all dataframes in list
time <- 1:120 #10 years.
extract_column <- lapply(lista_meses, function(x) x["revenue"])
fandataq <- do.call("cbind", extract_column)
mandataq <- as.matrix.data.frame(fandataq)
pdataq <- data.frame(x=time, t(fandataq)) %>% gather(key=sim, value=y, -x)
#Plotting ALL SIMULATIONS
ggplot(pdataq, aes(x=x, y= y, group = sim, colour = sim)) +
geom_line(show.legend = FALSE)
这就是我得到的情节,我不明白为什么那个区域应该有很多颜色和很多线条时完全是粉红色的。有谁知道我该如何解决这个问题?提前致谢!
解决方案
推荐阅读
- java - 我的简单 webview 应用程序现在在 googleplayconsole 上暂停,我该如何再次发布或删除它。?
- javascript - Openlayers 地图比例线相同的缩放率在公制中重复两次,即使缩放不同
- python-3.x - 更改发送到对话流代理的请求格式
- haskell - GHCi中的“return 1”如何显示“1”?
- java - Java JXBrowser:是否可以绘制 JXBrowser 组件?
- javascript - 在 MacOS 中使用 NodeJS 连接到 Informix - IBM 驱动程序问题
- r - 在 R 中使用 for 之前如何选择要创建的空变量
- android - Android Studio 中的 bin/ 文件夹
- python - 从文件中剥离数据
- python-3.x - 有没有人尝试过从 Python 运行嵌入在 Microsoft Word 文档中的宏