An exmaple.
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.