1

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))
}
3
  • Try something like: pdf("myPlot.pdf"); for(i in negbinom_list){print(VolPlot(get(i), i))}; dev.off() Commented Sep 12, 2019 at 9:19
  • In your function, I think there is a typo: VolPlot_name <- ggplot(data=data Commented Sep 12, 2019 at 9:22
  • I would add gg_list <- list() and then in your for loop do: gg_list[[i]] <- VolPlot(...) Commented Sep 12, 2019 at 9:54

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response MM! Unfortunately not quite there yet. I ran your code with the following change (due to an unexpected “)”) list_plots[count] <- VolPlot(get(i), i) Got these warnings: Warning messages: 1: In list_plots[count] <- VolPlot(get(i), i) : number of items to replace is not a multiple of replacement length 2: In list_plots[count] <- VolPlot(get(i), i) : number of items to replace is not a multiple of replacement length And creation of “list_plots” in my environment, which contained two data frames
I'm guessing those two data frames in list_plots should be plots, or maybe I'm not extracting them the correct way, plot(list_plots[[2]]) for example, returns plot's interpretation of the data frame
When I return the ggplot-objects as a list, too, it seems to solve the problem. I have changed the code above to solve the error message. Let me know whether it worked for you.
Thats working great now, thanks so much for the assistance! Helps with my understanding of loops in r also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.