R/bubbleplot.R

Defines functions bubbleplot lbubbleplot

Documented in bubbleplot lbubbleplot

#'  bubble plot for data
#'
#'
#' @param data  A list of data frames which have same structure.
#' @param x A x-axis variable,character type
#' @param y A y-axis variable,character type
#' @param size  the size of response,character type
#' @param save Save a png file using name as save.
#' @param facet Either a formula or character vector to wraps a 1d sequence of panels into 2d. Use either a one sided formula, ~a + b, or a character vector, c("a", "b").
#' @param color the color of the point
#' @param ylim the scale of y-axis, c(low, high).
#' @param xlab the label of x-axis, character type.
#' @param ylab the label of y-axis, character type.
#' @param title if there are multi-plots will be drawn, this list includes the title of each plot.
#' @param main a main title of plot.
#' @param range a numeric vector of length 2 that specifies the minimum and maximum size of the plotting symbol after transformation.
#' @param breaks A numeric vector of positions
#' @examples
#'    data(bubble)
#'    bubbleplot(bubble,x="time",y="value",size="HAMD",facet="variable")
bubbleplot<-function(data,x,y,size,facet=NULL,ylim=NULL,xlab=NULL,ylab=NULL,titles=NULL,main=NULL,range=NULL,breaks = ggplot2::waiver(),labels=ggplot2::waiver(),legend=NULL)
{
    gplot<- ggplot2::ggplot(data, ggplot2::aes_string(x = x, y = y, size = size)) +
        ggplot2::geom_point(shape = 21, colour = "black",fill = "grey30")+
        ggplot2::theme_bw()+ggplot2::scale_x_discrete(position = "top") +
        ggplot2::labs(x = xlab, y = ylab)+
        ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
    if(!is.null(facet))
    {
        gplot<- gplot + ggplot2::facet_wrap(facet,scales=ifelse(is.null(ylim),"free","fixed"),labeller = ggplot2::as_labeller(titles))
        gplot<- gplot + ggplot2::theme(axis.text.x = ggplot2::element_text(size=8))
    }
    if(!is.null(ylim))
    {
        gplot<- gplot + ggplot2::coord_cartesian(ylim = ylim)
    }
    if(!is.null(main))
    {
        gplot<- gplot + ggplot2::ggtitle(main)
    }
    if(is.null(legend))
    {
        legend<-size
    }
    if(is.null(range))
    {
        gplot<-gplot+ggplot2::scale_size(breaks = breaks, labels=labels,name = legend)
    }else
    {
        gplot<-gplot+ggplot2::scale_size(breaks = breaks, labels=labels,range = range, name = legend)
    }
    gplot
}




#'  bubble plot for data
#'
#'
#' @param data  A list of data frames which have same structure.
#' @param x A x-axis variable,character type
#' @param y A y-axis variable,character type
#' @param size  the size of response,character type
#' @param save Save a png file using name as save.
#' @param ylim the scale of y-axis, c(low, high).
#' @param xlab the label of x-axis, character type.
#' @param ylab the label of y-axis, character type.
#' @param title if there are multi-plots will be drawn, this list includes the title of each plot.
#' @param main a main title of plot.
#' @param range a numeric vector of length 2 that specifies the minimum and maximum size of the plotting symbol after transformation.
#' @param breaks A numeric vector of positions
#' @examples
#'    data(bubble)
#'    head(bubble)
#'    data_sort_by_variables<-split(bubble,bubble$variable)
#'    BubblePlot(data_sort_by_variables,"time","value","HAMD")
#'
lbubbleplot<-function(data,x,y,size,save=NULL,ylim=NULL,xlab=NULL,ylab=NULL,title=NULL,main=NULL,range=c(2,4.5),breaks = c(8.59, 11.37, 21.91))
{
    plotlist<-list()
    for(i in 1:length(data))
    {
        tmp<-data[[i]]
        plotlist[[i]] <- ggplot(tmp, aes_string(x = x, y = y, size = size)) +
            geom_point(shape = 21, colour = "black",fill = "grey30")+
            theme_bw()+scale_x_discrete(position = "top") +
            labs(x = xlab, y = ylab)+
            theme(plot.title = element_text(hjust = 0.5))+
            scale_size_continuous(range = range, breaks = breaks, labels=as.character(breaks))
        if(!is.null(ylim))
        {
            plotlist[[i]]<- plotlist[[i]]+ coord_cartesian(ylim = ylim)
        }
        if(!is.null(title))
        {
            plotlist[[i]]<- plotlist[[i]] +ggtitle(title[i])
        }
    }
    g<-grid.arrange(grobs=plotlist,top=main,
                    ncol=min(length(data),3),as.table=TRUE)
    if(!is.null(save))
    {
        ggsave(g, file=save, width = 15, height = 15)
    }
    return(plotlist)
}
ShouyeLiu/metaboliteUtility documentation built on May 6, 2019, 9:07 a.m.