R/plot_scatter.R

#' Plot log2 protrusion/cell-bodies distribution across two biological conditions
#'
#' \code{plot_scatter} generates a scatterplot for two conditions of interest
#' highlighting a given category of interest.
#'
#' @param se SummarizedExperiment,
#' Data object containing processed proteomics data
#' @param xpos Integer,
#' Timepoint to represent on the x axis on the scatterplot
#' @param ypos Integer,
#' Timepoint to represent on the y axis on the scatterplot
#' @param category Character,
#' Sets the proteins to highlight in the plot
#' @param color_highlight Character,
#' Sets the color of category to highlight in the plot
#' @param xlimit Character,
#' Sets the limit of x axis on the scatterplot
#' @param ylimit Character,
#' Sets the limit of y axis on the scatterplot
#' @examples
#' if(interactive()){
#' plot_scatter(se, 1, 2, "HIST", 'orange', 4, 4)
#' }
#' @return A scatter plot (generated by \code{\link[ggplot2]{ggplot}})
#' @import stringr
#' @import SummarizedExperiment
#' @importFrom janitor clean_names
#' @import tibble
#' @import ggplot2
#' @export
plot_scatter <- function (se,
                          xpos,
                          ypos,
                          category,
                          color_highlight,
                          xlimit,ylimit){

  if(is.integer(xpos)) x <- as.numeric(xpos)
  if(is.integer(ypos)) y <- as.numeric(ypos)
  if(is.integer(xlimit)) x <- as.numeric(xlimit)
  if(is.integer(ylimit)) y <- as.numeric(ylimit)

  assertthat::assert_that(inherits(se, "SummarizedExperiment"),
                          is.numeric(xpos),
                          length(xpos) == 1,
                          is.numeric(ypos),
                          length(ypos) == 1,
                          is.numeric(ylimit),
                          length(ylimit) == 1,
                          is.numeric(xlimit),
                          length(xlimit) == 1,
                          is.character(category))

highlight_df <- rownames_to_column(as.data.frame(assay(se)),
                                   var="protein_names") %>%
  as_tibble()  %>%
  filter(grepl(category, protein_names)) %>% clean_names()

pos_x<-paste0("x",colnames(assay(se))[xpos])
pos_y<-paste0("x",colnames(assay(se))[ypos])

x_axis<-paste0(str_split(colnames(assay(se))[xpos],"_",simplify = TRUE)[1], " min log2 (Prot/Body)")
y_axis<-paste0(str_split(colnames(assay(se))[ypos],"_",simplify = TRUE)[1], " min log2 (Prot/Body)")


rownames_to_column(as.data.frame(assay(se)),
                   var="protein_names") %>%
  clean_names() %>% as_tibble() %>%
  ggplot()+
  geom_point(aes(get(pos_x),get(pos_y)), shape=0, color='black')+
  geom_point(data=highlight_df,
             aes(get(pos_x),get(pos_y)),
             color=color_highlight,
             shape=0,
             size=2)+
  geom_vline(xintercept = 0)+ geom_hline(yintercept = 0)+
  coord_cartesian(xlim=c(-xlimit,xlimit),ylim=c(-ylimit,ylimit))+
  labs(x=x_axis,y=y_axis)

}
demar01/protrusionproteome documentation built on April 29, 2021, 5:47 a.m.