Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem creation multiples ggplot with a list of dataframes, stored in a list of plot

I have this list of dataframes
enter image description here

i want to generate the same plot for each of dataframe. (the columns of interest share the same name, that are : " COType ; Zscore ; SpatialLag ".
Here’s my code plotting a single dataframe (Community) :

ggplot(List_df_EU[["COMMUNITY"]], aes(x = Zscore, 
                       y = SpatialLag,
                       fill = COType)) + 
  geom_point(color = "white", shape = 21, size = 2) + 
  theme_minimal() + 
  geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) + 
  geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) + 
  scale_fill_manual(values = color_values) + 
  labs(title = names(List_df_EU[1]),
       subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
       x = "z-score",
       y = "Spatial lag",
       fill = "Type de voisinage")+
  theme_fivethirtyeight()+
  theme (axis.title = element_text(), text = element_text(family = myFont1))

enter image description here
Here’s the code supposed to create a single plot for each dataframe. it doesn’t fill the list and doesn’t send error message back

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

plotlist <- vector()

plotlist <- for(i in 1:length(List_df_EU)) {                              
 ggplot(List_df_EU[[i]], aes(x = Zscore, 
                                      y = SpatialLag,
                                      fill = COType)) + 
  geom_point(color = "white", shape = 21, size = 2) + 
  theme_minimal() + 
  geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) + 
  geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) + 
  scale_fill_manual(values = color_values) + 
  labs(title = names(List_df_EU[[i]]),
       subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
       x = "z-score",
       y = "Spatial lag",
       fill = "Type de voisinage")+
  theme_fivethirtyeight()+
  theme (axis.title = element_text(), text = element_text(family = myFont1))
} 

If you have any tips it would be awesome!(Would be deseperate move generating plots manually)

>Solution :

To run the same ggplot code over a list of dataframes, I think lapply is better than a for loop. Try this:

plotlist <- lapply(List_df_EU, function(x) {
  ggplot(x, aes(x = Zscore, 
                y = SpatialLag,
                fill = COType)) + 
    geom_point(color = "white", shape = 21, size = 2) + 
    theme_minimal() + 
    geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) + 
    geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) + 
    scale_fill_manual(values = color_values) + 
    labs(title = names(x),
         subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
         x = "z-score",
         y = "Spatial lag",
         fill = "Type de voisinage")+
    theme_fivethirtyeight()+
    theme (axis.title = element_text(), text = element_text(family = myFont1))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading