R/brokenlineplot.R

Defines functions brokenlineplot

Documented in brokenlineplot

#'  brokenline 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 group  the groups of response,character type
#' @param xlim the scale of x-axis, c(low, high).
#' @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 shape a list of point shape match to groups
#' @param color a list of color match to group
#' @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.
#' @examples
#'    library(ggplot2)
#'    data("economics")
#'    test<-economics
#'    test$group<-"a"
#'    test[which(a$uempmed<15),"group"]<-"b"
#'    brokenlineplot(test,x="psavert",y="uempmed",groups="group")
brokenlineplot<-function(data, x, y, groups=NULL,xlab=NULL,ylab=NULL,main=NULL,xlim=NULL,ylim=NULL, color="black",shape=NULL, line="solid",facet=NULL,titles=NULL)
{
    gplot <-ggplot2::ggplot(data=data,ggplot2::aes_string(x=x,y=y, group=ifelse(is.null(groups),"1",groups)))
    if(!is.null(groups))
    {
        gplot<-gplot+ggplot2::geom_point(ggplot2::aes_string(shape=groups))+
            ggplot2::geom_line(ggplot2::aes_string(colour=groups),linetype=line)
    }
    else
    {
        gplot<-gplot+ggplot2::geom_point(colour=color)+
            ggplot2::geom_line(linetype=line,colour=color)
    }
    gplot<-gplot+   ggplot2::ylab(ylab)+
        ggplot2::xlab(xlab)+
        ggplot2::theme_bw()+
        ggplot2::theme(legend.position="right",plot.title = ggplot2::element_text(hjust = 0.5))+
        ggplot2::coord_cartesian(xlim = xlim,ylim = ylim)
    if(!is.null(facet))
    {
        gplot<- gplot +ggplot2::facet_wrap(facet,ncol  = 3,scales=ifelse(is.null(xlim)||is.null(ylim),"free","fixed"),labeller = ggplot2::as_labeller(titles))
    }
    if(color!="black")
    {
        gplot<- gplot +ggplot2::scale_colour_manual(values = color)
    }
    if(!is.null(shape))
    {
        gplot<- gplot +ggplot2::scale_shape_manual(values = shape)
    }
    if(!is.null(main))
    {
        gplot<- gplot + ggplot2::ggtitle(main)
    }
    gplot
}
ShouyeLiu/metaboliteUtility documentation built on May 6, 2019, 9:07 a.m.