Ggplot objects stored using a for loop are all identical

I am trying to visualize this dataset using the packages from the tidyverse collection: https://www.kaggle.com/ruslankl/mice-protein-expression

After importing the dataset, I tidied the data like so:

library(tidyverse)
#> Creating a table with the average expression level of each protein for each experimental group
DCN_byclass = group_by(Data_Cortex_Nuclear, class, Genotype) %>%
  select(-Behavior, -Treatment, -MouseID) %>%
  summarise_each(funs(mean(., na.rm = TRUE)))
#> Adding descriptions to each experimental group
classes = c('c-CS-s','c-CS-m','c-SC-s','c-SC-m','t-CS-s','t-CS-m','t-SC-s','t-SC-m')
desc = c('control mice, stimulated to learn, injected with saline',
         'control mice, stimulated to learn, injected with memantine',
         'control mice, not stimulated to learn, injected with saline',
         'control mice, not stimulated to learn, injected with memantine',
         'trisomy mice, stimulated to learn, injected with saline',
         'trisomy mice, stimulated to learn, injected with memantine',
         'trisomy mice, not stimulated to learn, injected with saline',
         'trisomy mice, not stimulated to learn, injected with memantine')
class_desc = tibble(class = classes, description = desc)
DCN_byclass = left_join(DCN_byclass, class_desc, by = 'class')
#> Rearranging the table in a more logical order
DCN_byclass = select(DCN_byclass, class, description, Genotype, everything())

Afterwards, I wished to make 77 bar plots, plotting the variable, Genotype, against 77 other variables in the dataset, making an individual plot for each pair. I tried to accomplish this by making a for loop that would create these plots and store them in an empty list:

#> Creating a list that contains each protein and its expression levels as separateitems
columns = list()
for (i in 1:77) {
columns[i] = DCN_byclass[,(i+3)]
}
#> Generating an empty list to store plots in
plots = list()
#> Creating 77 bar plots, each with the experimental groups plotted against an individual protein's expression levels
for (i in 1:77) {
yvalues = columns[[i]]
plots[[i]] = ggplot(data = DCN_byclass, mapping = aes(Genotype, y = yvalues, fill = class)) +
  geom_col(position = 'dodge')
}

I then tried viewing one of the plots by subsetting the list, like so:

plots[1]

And this plot was successfully shown:
Genotype vs. Protein Expression
However, it seems the same plot is produced by subsetting the list, plots, with any other number. All 77 plots in the list are identical. Does anyone know why this might be?

2 Likes

Without fully understanding the structure of your data, here is an example of how I'd approach something like this:

library(tidyverse)
library(patchwork)
plot_list <- diamonds %>% 
  sample_n(1000) %>% 
  split(.$color) %>% 
  map(~{
    .x %>% 
      group_by(cut,clarity) %>% 
      summarize(price = mean(price)) %>% 
      ggplot(aes(cut,price,fill = clarity))+
      geom_col(position = "dodge")
  })
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)
#> `summarise()` regrouping output by 'cut' (override with `.groups` argument)



plot_list[[1]] / plot_list[[2]]

Created on 2020-07-19 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.