####################Plotting means and error bars
rm(list = ls())
library(ggplot2)
library(plotrix) #for std.error
tg <- ToothGrowth
head(tg)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5
table(tg$supp,tg$dose)
##     
##      0.5  1  2
##   OJ  10 10 10
##   VC  10 10 10
str(tg)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
tgc <- aggregate(tg[1],list(supp = tg$supp, dose = tg$dose),
                 FUN = function(x) c(mean = mean(x), se = std.error(x)))
str(tgc)
## 'data.frame':    6 obs. of  3 variables:
##  $ supp: Factor w/ 2 levels "OJ","VC": 1 2 1 2 1 2
##  $ dose: num  0.5 0.5 1 1 2 2
##  $ len : num [1:6, 1:2] 13.23 7.98 22.7 16.77 26.06 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:2] "mean" "se"
tgc2 <- cbind(tgc,tgc$len)
colnames(tgc2)
## [1] "supp" "dose" "len"  "mean" "se"
tgc2$dose <- factor(tgc2$dose)
####################################plot
ggplot(tgc2, aes(x=dose, y=mean, fill=supp)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black", # Use black outlines
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(0.9)) +
  xlab("Dose (mg)") +
  ylab("Tooth length") +
  ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +
   scale_fill_manual(name="Supplement type", # Legend label, use darker colors
                     breaks=c("OJ", "VC"),
                     labels=c("Orange juice", "Ascorbic acid"),
                     values=c("#CCCCCC","#FFFFFF"))+
  scale_y_continuous(expand = c(0,0),breaks = seq(0,30,5),limits = c(0,30)) +
  theme(legend.position = "right",
        legend.direction = "vertical",
        legend.spacing.x = unit(0.1, 'cm'),
        legend.spacing.y = unit(0.2, 'cm'),
        legend.title.align = 0.1,
        legend.key.size = unit(.5, "cm"),
        legend.text = element_text(colour="blue", size=10, 
                                   face="bold"),
        legend.title = element_text(colour="red", size=10, 
                                    face="bold"),
        legend.background = element_rect(fill="lightblue",
                                         size=2, linetype="solid", 
                                         colour ="blue"),
        legend.key.height=unit(1.2,"line"),
        legend.key.width=unit(1.2,"line"),
        #legend.margin=margin(5,5,5,5),
        legend.justification = c(0, 1),
        legend.box.margin=margin(0,0,0,0),
        panel.background = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        panel.grid = element_blank(),
        axis.text.x   = element_text(size= 12, color = "darkblue",family = "sans",hjust = 0.5),
        axis.text.y   = element_text(size= 12, color = "darkblue",family = "sans",vjust = 0.5,hjust = 0.5),
        axis.title  = element_text(size=12, color = "darkred",family = "sans"),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"))

#output p1
ggsave(paste0(Sys.Date(),"-plot21.tiff"), 
       plot = last_plot(), device = NULL, 
       scale = 1, width = 10, height = 10, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")

#ref
#http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/