10 plot a day - day 6

Stacked bar graph

Data: cabbage_exp from gcookbook

head(cabbage_exp)
##   Cultivar Date Weight        sd  n         se
## 1      c39  d16   3.18 0.9566144 10 0.30250803
## 2      c39  d20   2.80 0.2788867 10 0.08819171
## 3      c39  d21   2.74 0.9834181 10 0.31098410
## 4      c52  d16   2.26 0.4452215 10 0.14079141
## 5      c52  d20   3.11 0.7908505 10 0.25008887
## 6      c52  d21   1.47 0.2110819 10 0.06674995
str(cabbage_exp)
## 'data.frame':    6 obs. of  6 variables:
##  $ Cultivar: Factor w/ 2 levels "c39","c52": 1 1 1 2 2 2
##  $ Date    : Factor w/ 3 levels "d16","d20","d21": 1 2 3 1 2 3
##  $ Weight  : num  3.18 2.8 2.74 2.26 3.11 1.47
##  $ sd      : num  0.957 0.279 0.983 0.445 0.791 ...
##  $ n       : int  10 10 10 10 10 10
##  $ se      : num  0.3025 0.0882 0.311 0.1408 0.2501 ...
summary(cabbage_exp)
##  Cultivar  Date       Weight            sd               n     
##  c39:3    d16:2   Min.   :1.470   Min.   :0.2111   Min.   :10  
##  c52:3    d20:2   1st Qu.:2.380   1st Qu.:0.3205   1st Qu.:10  
##           d21:2   Median :2.770   Median :0.6180   Median :10  
##                   Mean   :2.593   Mean   :0.6110   Mean   :10  
##                   3rd Qu.:3.033   3rd Qu.:0.9152   3rd Qu.:10  
##                   Max.   :3.180   Max.   :0.9834   Max.   :10  
##        se         
##  Min.   :0.06675  
##  1st Qu.:0.10134  
##  Median :0.19544  
##  Mean   :0.19322  
##  3rd Qu.:0.28940  
##  Max.   :0.31098

stacked bar graph

ggplot(cabbage_exp, aes(x=Date, y = Weight, fill = Cultivar)) +
        geom_bar(stat = "identity")

reverse the legend order

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
        geom_bar(stat = "identity") +
        guides(fill = guide_legend(reverse = TRUE))

reverse the stacked order

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar, order = desc(Cultivar))) + 
        geom_bar(stat = "identity")

using a different palette with black outlines

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
        geom_bar(stat = "identity", colour = "black") + 
        guides(fill = guide_legend(reverse = TRUE)) + 
        scale_fill_brewer(palette = "Pastel1")

100% stacked bar plot

# scale data to 100% within each stack
ce <- ddply(cabbage_exp, "Date", transform, percent_weight = Weight/sum(Weight) *100)
ce
##   Cultivar Date Weight        sd  n         se percent_weight
## 1      c39  d16   3.18 0.9566144 10 0.30250803       58.45588
## 2      c52  d16   2.26 0.4452215 10 0.14079141       41.54412
## 3      c39  d20   2.80 0.2788867 10 0.08819171       47.37733
## 4      c52  d20   3.11 0.7908505 10 0.25008887       52.62267
## 5      c39  d21   2.74 0.9834181 10 0.31098410       65.08314
## 6      c52  d21   1.47 0.2110819 10 0.06674995       34.91686
# stacked barplot
ggplot(ce, aes(x = Date, y = percent_weight, fill = Cultivar)) + 
        geom_bar(stat = "identity")

improved 100% stacked bar plot

ce <- ddply(cabbage_exp, "Date", transform, percent_weight = Weight/sum(Weight)*100)
ggplot(ce, aes(x = Date, y = percent_weight, fill = Cultivar)) + 
        geom_bar(stat = "identity", colour = "black") + 
        guides(fill = guide_legend(reverse = TRUE)) + 
        scale_fill_brewer(palette = "Pastel1")

add labels to bar graph

Below the top

ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
        geom_bar(stat = "identity") + 
        geom_text(aes(label = Weight), vjust = 1.5, color = "white")

Above the top

ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
        geom_bar(stat = "identity") + 
        geom_text(aes(label = Weight), vjust = -2)

We could see label on the first bar is above the y limits

Adjust y limits to be higher

ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
        geom_bar(stat = "identity") + 
        geom_text(aes(label = Weight), vjust = -2) + 
        ylim(0, max(cabbage_exp$Weight) + 0.5)

Or directly change the label position by mapping y to a little bit higher

ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
        geom_bar(stat = "identity") + 
        geom_text(aes(y = Weight + 0.1, label = Weight))