R/forestplot.R

Defines functions forestplot

Documented in forestplot

#' Forest 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 xmin A lower confidence level variable,character type.
#' @param xmax A lower confidence level variable,character type.
#' @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 save Save a png file using name as save.
#' @param Aesthetic_names A variable determines aesthetics including color and shape,character type.
#' @param shape a list of point shape match to Aesthetic_names.
#' @param color a list of color match to Aesthetic_names.
#' @param xlim the scale of x-axis, c(low, high).
#' @param xlab the label of x-axis, character type.
#' @param title if there are multi-plots will be drawn, this list includes the title of each plot, eg., c(`0` = "Zero", `1` = "One").
#' @param main a main title of plot.
#' @examples
#'    data(Forest)
#'    head(Forest)
#'    forestplot(Forest,x="Estimate",y="Var",xmin = "Conf.low", xmax = "Conf.high",Aesthetic_names = "P")

forestplot<-function(data,x,y,xmin,xmax,save=NULL,facet=NULL,Aesthetic_names=NULL,shape=NULL,color=NULL,xlim=NULL,xlab=NULL,ylab=NULL,titles=NULL,main=NULL)
{
    gplot <-ggplot2::ggplot(data=data,ggplot2::aes_string(x=x,y=y, color=Aesthetic_names))+
        ggplot2::geom_point(ggplot2::aes_string(shape=Aesthetic_names))+
        ggplot2::geom_errorbarh(ggplot2::aes_string(xmin=xmin,xmax=xmax, fill=Aesthetic_names),height=0.5)+
        ggplot2::geom_vline(xintercept=1,linetype="solid")+
        ggplot2::ylab(ylab)+
        ggplot2::xlab(xlab)+
        ggplot2::theme_bw()+
        ggplot2::scale_x_continuous(position = "top")+
        ggplot2::theme(legend.position="bottom")+
        ggplot2::coord_cartesian(xlim = xlim)
    if(!is.null(facet))
    {
        gplot<- gplot +ggplot2::facet_wrap(facet,scales=ifelse(is.null(xlim),"free","fixed"),labeller = ggplot2::as_labeller(titles))
    }
    if(!is.null(main))
    {
        gplot<- gplot +ggtitle(main)
    }
    if(!is.null(color))
    {
        gplot<- gplot +ggplot2::scale_colour_manual(values = color)
    }
    if(!is.null(shape))
    {
        gplot<- gplot +ggplot2::scale_shape_manual(values = shape)
    }

    return(gplot)
}
ShouyeLiu/metaboliteUtility documentation built on May 6, 2019, 9:07 a.m.