This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to print individual ggplot after rbind and inside the nested loop

In this code I want to draw ggplot inside the loop for each alpha and y axis takes the ylim(min(Pro_df$Relative_Error),max(Pro_df$Relative_Error)), each alpha in a graph individually, that's mean I want 7 ggplot. Also, I want geom_boxplot individually in a graph for each alpha. I tried to do that by the following code but it did not work.

library(ggplot2)
library(gganimate)

Pro_df <- data.frame(
x = integer(0),
Alpha = numeric(0), 
Relative_Error = numeric(0))
mu=7      # Mean Value
sigma2=4   # Variance value
for (alpha in c(0.001,0.01,0.025,0.05,0.1,0.25,0.375))  {
for (i in 1:13) 
{
E_PDF=dnorm(i,mean=mu,sd=sqrt(sigma2))

Relative_Error=(5-E_PDF)/(1-E_PDF) 

newrow <- data.frame(x = i, 
                     Alpha = alpha, 
                     Relative_Error = Relative_Error)

Pro_df <- rbind(Pro_df, newrow)
  }

all the previous code work correctly, now I want to plot my ggplot and boxplot so before close the first loop I wrote the following code but it did not work as I want in my question above.

print(map2 <- ggplot() +
geom_boxplot(data = Pro_df, 
             aes( , y =Relative_Error),
             colour = "red", size = .5))      
print(ggplot(Pro_df, aes(x =x, y =Relative_Error, colour = Alpha)) +
      geom_line() +
      ylim(min(Pro_df$Relative_Error),max(Pro_df$Relative_Error)))
}
rstudio ggplots

Can you explain in more detail what you want your final plot to look like. Your description and code is a bit confusing.

I have a long program to approximation some distribution by saddlepoint of the trimmed mean in 7 value of alpha and compare this method with the exact distribution and compute the relative error between the exact pdf with the approximation pdf. finally I draw the relative error to check of my method is good or not. So I need to create a list of plots to store my plots and print my plots individual at each alpha after compute my distribution in the r.v. from 1:13. Each plots takes the x value and relative error value. Because the program is very long so i cut my calculations and put just 5 in the relative error equation.

So each individual plot will be one of the Alpha values tested, with the x-axis values being the integer values from the 'x' column, and the y-axis your relative error values? That means each one of your x values will have only one relative error value associated with it, so doing a boxplot wouldn't make sense in this configuration. Did you perhaps mean a bar plot instead?

1 answer

I believe this is what you wanted. If it isn't post with a comment to clarify the problem and I can edit my answer.

library("tidyverse")

Pro_df %>%
  mutate(Alpha=as_factor(Alpha)) %>%
  ggplot(aes(x=x, y=Relative_Error)) +
    geom_col(width=0.85, aes(fill=Alpha)) +
    geom_line() +
    facet_wrap(.~Alpha, ncol=3) +
    coord_cartesian(ylim=c(
      min(Pro_df$Relative_Error),
      max(Pro_df$Relative_Error))
    ) +
    scale_fill_viridis_d(end=0.95, guide=FALSE)

enter image description here

I want every plot on a single page.

Log in to answer this question.