Search code examples
rdataframeggplot2plotreturn

ggplot within a function does not return a scatterplot with datapoints, instead a plot with dataframe values. How to fix this?


I'm writing a function where I should get 2 ggplots objects returned to me in RStudio based on two different dataframes generated within my function. However, instead I get a plot with all the dataframe values "printed" in it returned and not a normal scatterplot.

I tried:

  1. return(list(df1, df2))
  2. Plots<- list(df1, df2), return(Plots)
  3. View(df1) View(df2)
  4. ggplot without storing it into an object
  5. Just return a single ggplot and not using list() to return two.
  6. Print() instead of return or view.

Every result has the same outcome (picture):  As you can see on the bottom right, I do not get a scatter plot. The console does show output [1] and [[2]], but nothing else. The code itself is working perfectly. I ran debug, I've got no errors and above all when I replaced ggplot with plot(), this DID return the prefered scatterplot to me. So I assume the problem is not related to the code itself.

However, I am much more familiar with customizations with ggplot than plot(), so if anyone knows how to solve this issue it would be amazing. Provided below I added some sample data and some sample code, although I'm not sure whether that is relevant with this issue.

The code I used within my function to create and return the ggplots is:

MD_filter_trial<- function(dataframe, mz_col, a = 0.00112, b = 0.01953){ 
 MZ<- mz_col
 MZR<- trunc(mz_col, digits = 0)#Either floor() or trunc() can be used for this part.
 MD<- as.numeric(MZ-MZR)
 MD.limit<- b + a*mz_col
 dataframe<- dataframe%>%
   dplyr::mutate(MD, MZ, MD.limit)%>%
   dplyr::select(MD, MZ, MD.limit)
 
 highlight_df <- dataframe %>% filter(MD >= MD.limit) #Notice how this is the exact opposite from the 
 
 MD_plot<- ggplot(data=dataframe, aes(x=MZ, y=MD))+
 geom_point()+
 geom_point(data=highlight_df, aes(x=MZ,y=MD), color='red')+#I added this one, so the data which will be removed will be highlighted in red.
 ggtitle(paste("Unfiltered MD data - ", dataframe))
 
 filtered<- dataframe%>%
   filter(MD <= MD.limit)# As I understood: Basically all are coordinates. The maxima equation basically gives coordinates 
   
 MD_plot_2<- ggplot(data=filtered, aes(x=MZ, y=MD))+ #Filtered is basically the second dataframe, #which subsets datapoints with an Y value (which is the MD), below the linear equation MD...
 geom_point()+
 ggtitle(paste("Filtered MD data - ", dataframe))
 
 N_Removed_datapoints <- nrow(dataframe) - nrow(filtered)

 print(paste("Number of peaks removed:", N_Removed_datapoints))
 MD_PLOTS<-list(dataframe, filtered, MD_plot, MD_plot_2)
 return(MD_PLOTS)
}
Sample data:

structure(list(mz_col= c(99.0001, 99.0056, 99.0079, 99.0097, 99.0105, 
99.0116, 99.0158, 99.0169, 99.019, 99.0196, 99.0207, 99.0215, 
99.0239, 99.0252, 99.026, 99.0269, 99.0288, 99.0295, 99.0302, 
99.0311, 99.0318, 99.0332, 99.034, 99.0346, 99.0355, 99.0376, 
99.039, 99.04, 99.0405, 99.0414, 99.0421, 99.043, 99.0444, 99.0473, 
99.048, 99.0517, 99.0536, 99.0547, 99.0556, 99.057, 99.0575, 
99.0586, 99.0599, 99.0606, 99.0621, 99.0637, 99.0652, 99.0661, 
99.0668, 99.0686, 99.0694, 99.0699, 99.0707, 99.0714, 99.072, 
99.075, 99.0762, 99.0794, 99.0808, 99.0836, 99.0888, 99.0901, 
99.0911, 99.092, 99.095, 99.0962, 99.1001, 99.1064, 99.1173, 
99.4889, 99.5059, 99.5084, 99.5126, 99.5158, 99.5165, 99.5173, 
99.5183, 99.526, 99.5266, 99.5315, 99.5345, 99.5358, 99.5402, 
99.543, 99.5472, 99.548, 99.5529, 99.5572, 99.5577, 99.9408, 
99.9551, 99.9599, 99.9646, 99.9718, 99.9887)), row.names = c(NA, 
-95L), class = c("tbl_df", "tbl", "data.frame"))


Solution

  • In your ggtitles calls perhaps you mean:

    ggtitle(paste("Filtered MD data -", deparse(substitute(dataframe)))
    

    Within a function this takes the name of the object passed to the dataframe argument and pastes it into a string, rather than putting the whole dataframe in.