Gordon Beattie
Gordon Beattie

Reputation: 135

for loop not creating multiple ggplot objects

I'm hoping to create a ggplot function which loops through a list of objects, and creates individual ggplot objects. The code below successfully prints out the last plot in the list, but nothing is saved as an object.

Thanks in advance for any assistance!

negbinom_list <- c("cytoxic_CD4_CD8.wt.negbinom", "helper_CD4.wt.negbinom")


VolPlot <- function(data, title){

highlight_df <- data %>% filter(p_val_adj<=0.05) %>% filter(avg_logFC<=-0.5 | avg_logFC>=0.5)
data$ID <- row.names(data)
label_df <- filter(data, ID %in% GOI)
data$ID <- row.names(data)
VolPlot_name <- paste( 'volplot', title, sep = '.' )
VolPlot_name <- ggplot(data=data, 
            aes(x=avg_logFC, y =-log10(p_val_adj))) +
  geom_point(alpha=0.4, size=1.75) +
  xlim(c(-2, 2)) +
  xlab("log2 fold change") + ylab("-log10 adjusted p value") +
  theme_bw() +
  theme(legend.position="none") +
  geom_point(data=highlight_df, aes(x=avg_logFC, y =-log10(p_val_adj)), color='red', alpha=0.4,size=1.75) +
  geom_text(aes(label=ifelse(avg_logFC<=-0.75 | avg_logFC>=0.75,as.character(ID),'')),hjust=0,vjust=0) +
  geom_label_repel(data = label_df, aes(label = ID), nudge_y = 36, nudge_x = 1, direction = "x")
}
for(i in negbinom_list){
  print(VolPlot(get(i), i))
}

Upvotes: 1

Views: 132

Answers (1)

M M
M M

Reputation: 448

If you are planning to save each ggplot-object into a variable, it might be a better choice to return each ggplot object in a list at the end of the function with return(list(VolPlot_name)). Returning the object alone ends up with just the data frame inside the list.

In this way, you can create a list outside of your function. While you are looping through your variable negbinom_list, you can add each result into the new list.

Later on, you can access each ggplot-object in the list with g.e. new_list[[1]], etc.

Let me know whether it worked for you.

negbinom_list <- c("cytoxic_CD4_CD8.wt.negbinom", "helper_CD4.wt.negbinom")


VolPlot <- function(data, title){

highlight_df <- data %>% filter(p_val_adj<=0.05) %>% filter(avg_logFC<=-0.5 | avg_logFC>=0.5)
data$ID <- row.names(data)
label_df <- filter(data, ID %in% GOI)
data$ID <- row.names(data)
VolPlot_name <- paste( 'volplot', title, sep = '.' )
VolPlot_name <- ggplot(data=data, 
            aes(x=avg_logFC, y =-log10(p_val_adj))) +
  geom_point(alpha=0.4, size=1.75) +
  xlim(c(-2, 2)) +
  xlab("log2 fold change") + ylab("-log10 adjusted p value") +
  theme_bw() +
  theme(legend.position="none") +
  geom_point(data=highlight_df, aes(x=avg_logFC, y =-log10(p_val_adj)), color='red', alpha=0.4,size=1.75) +
  geom_text(aes(label=ifelse(avg_logFC<=-0.75 | avg_logFC>=0.75,as.character(ID),'')),hjust=0,vjust=0) +
  geom_label_repel(data = label_df, aes(label = ID), nudge_y = 36, nudge_x = 1, direction = "x")

return(list(VolPlot_name))
}

list_plots <- list()
count = 1
for(i in negbinom_list){
  list_plots[count] <- VolPlot(get(i), i)
  count = count + 1
}

Upvotes: 2

Related Questions