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))
}
pdf("myPlot.pdf"); for(i in negbinom_list){print(VolPlot(get(i), i))}; dev.off()
- zx8754VolPlot_name <- ggplot(data=data
- zx8754gg_list <- list()
and then in your for loop do:gg_list[[i]] <- VolPlot(...)
- Cole