How to visualize data from for loop using ggplot2

Im trying to plot the data from all 250 loops into one figure

The loop works on its own but i only see data from the first loop when i use ggplot. Do i need to include ggplot into the loop? Or do i maybe need to save the output from the loop into a vector/list? (output is a 2x131 data frame)

for(i in seq(250)){
  sim_data<- mvrnorm(n=100, mu, sigma) 
  mean1<- colMeans(sim_data) 
  cov <- cov(sim_data) 
  output <- data.frame(compute_efficient_frontier(cov,mean)) 
}
ggplot(output, aes(x = sd, y = mu))+
geom_point()

Hello rhonano,

It's difficult to understand what your code is doing as it appears you're using functions mvrnorm and compute_efficient_frontier which seems to be self-defined or part from an unknown package.

Could you please provide a reprex as explained below, so that we can assist you better?



#just one
  x = sample(1:10000,100,replace=TRUE)
  y= x*sample(1:250,1)
 plot(data.frame(x,y))

      
#many
output <- data.frame(x=NULL,y=NULL)
for(i in seq(250)){
 x = sample(1:10000,100,replace=TRUE)
 y= x*sample(1:250,1)
  output <- rbind(output,data.frame(x,y) )
}

plot(output)

This topic was automatically closed 21 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.