首页 > 解决方案 > 具有 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)

这就是我得到的情节,我不明白为什么那个区域应该有很多颜色和很多线条时完全是粉红色的。有谁知道我该如何解决这个问题?提前致谢! 在此处输入图像描述

标签: rggplot2

解决方案


推荐阅读