R/ggplot_errorbar.R

#' An Example for 'ggplot' with Errorbar
#'
#' @description An exmaple.
#' @export
#' @examples
#' library(R11)
#' # ggplot_errorbar()
#' meanx <- c(-20,-18,-15)
#' meany <- c(6,8,9)
#' sdx <- c(0.7, 1, 0.9)
#' sdy <- c(1.2, 1.5, 0.8)
#' Groups <- c('group1', 'group2', 'group3')
#' dat<-data.frame(meanx, meany, sdx, sdy, Groups)
#' View(dat)
#'
#' library(ggplot2)
#' cols<-c("grey10", "blue", "red")
#'
#' ##--------------------------------------------
#' #      Case1: geom_barplot and geom_errorbar
#' ##--------------------------------------------
#' p<- ggplot(data=dat,mapping = aes(x=dat$Groups, y=dat$meany, fill=factor(dat$Groups)))+
#'        geom_col(width=0.3)+
#'        scale_fill_manual(values = cols)+
#'        scale_y_continuous(limits = c(0,13), expand = c(0,0))+  # expand = c(0,0) means no space between ymin and x-axis.
#'        xlab('Groups')+ylab('x value')+ theme_bw()
#' p <- p + theme(panel.grid = element_blank())
#' p
#'
#' q <- p + geom_errorbar(aes(ymin=dat$meany-dat$sdy, ymax=dat$meany+dat$sdy), width=0.15)
#' q
#'
#' q + annotate(geom="text", x=dat$Groups[1], y=dat$meany[1]+dat$sdy[1]+0.5, label="b", color="black") +
#'     annotate(geom="text", x=dat$Groups[2], y=dat$meany[2]+dat$sdy[2]+0.5, label="a", color="black") +
#'     annotate(geom="text", x=dat$Groups[3], y=dat$meany[3]+dat$sdy[3]+0.5, label="a", color="black")
#'
#'
#' ##--------------------------------------------
#' #     Case2: geom_point and double errorbars
#' ##--------------------------------------------
#' p<- ggplot(data=dat,mapping = aes(x=dat$meanx, y=dat$meany))+
#'        geom_point(aes(colour=factor(dat$Groups)))+
#'        scale_color_manual(values = cols)+
#'        scale_y_continuous(limits = c(0,13), expand = c(0,0))+
#'        xlab('Groups')+ylab('x value')+ theme_bw()
#' p
#' q <- p +
#'     geom_errorbar(aes(ymin=dat$meany-dat$sdy, ymax=dat$meany+dat$sdy), width=0.3)+
#'     geom_errorbarh(aes(xmin=dat$meanx-dat$sdx, xmax=dat$meanx+dat$sdx), height=0.3)
#' q
#'
#' q + annotate(geom="text", x=dat$meanx[1], y=dat$meany[1]+dat$sdy[1]+0.5, label="b", color="black") +
#'     annotate(geom="text", x=dat$meanx[2], y=dat$meany[2]+dat$sdy[2]+0.5, label="a", color="black") +
#'     annotate(geom="text", x=dat$meanx[3], y=dat$meany[3]+dat$sdy[3]+0.5, label="a", color="black")
#'
#' # More links
#' # https://ggplot2.tidyverse.org/reference/geom_point.html
#' # https://ggplot2.tidyverse.org/reference/geom_linerange.html
#' # https://groups.google.com/forum/#!topic/ggplot2/aiJKyg8U7QM
#' @seealso \code{\link[ggplot2]}

ggplot_errorbar<-function(){?ggplot_errorbar}
PhDMeiwp/R11 documentation built on May 20, 2019, 4:25 p.m.