The following set of commands will print an appropriately formatted pdf file with the data represented, and this is the documentation for Dimplot. It runs appropriately and all the required packages load fine.
project_name<-"my_project"
file_stem<-" my_dim_plot_"
fig_name<-paste0(figure_dir, "/", file_stem, project_name, ".pdf")
base_plot<-DimPlot(combined_data_obj, reduction = "tsne", label = TRUE) + NoLegend()
ggsave(fig_name, width = 20, height = 16, units = "cm", device='pdf')
base_plot
dev.off()
Why, then, does this code print an empty pdf?
save_ggplot_figure<-function(plot_object, figure_dir, file_stem, project_name) {
# Need to add functionality for mutiple plots, different file extensions, etc.
fig_name<-paste0(figure_dir, "/", file_stem, project_name, ".pdf")
ggsave(fig_name, width = 20, height = 16, units = "cm", device='pdf')
plot_object
dev.off()
}
project_name<-"my_project"; file_stem<-"tsne_dim_plot_"; project_name<-"my_project"
base_plot<-DimPlot(combined_data_obj, reduction = "tsne", label = TRUE) + NoLegend()
save_ggplot_figure(base_plot, figure_dir, file_stem, project_name)
The only difference I can see is that the commands to print the plot then turn dev.off() are inside a function call, but why should this matter?
I tried the same code using pdf() instead of ggsave(), but with the same results (it prints a 4kb) empty pdf file instead of the 120kb pdf file printed when the commands are printed outside the function call.