Graphics Reference
In-Depth Information
Discussion
To calculate the percentages within each Weight group, we used the ddply() function. In the
example here, the ddply() function splits the input data frame, cabbage_exp , by the specified
variable, Weight , and applies a function, transform() , to each piece. (Any remaining argu-
ments in the ddply() call are passed along to the function.)
This is what cabbage_exp looks like, and what the ddply() call does to it:
cabbage_exp
Cultivar Date Weight sd n se
c39 d16
3.18 0.9566144 10 0.30250803
c39 d20
2.80 0.2788867 10 0.08819171
c39 d21
2.74 0.9834181 10 0.31098410
c52 d16
2.26 0.4452215 10 0.14079141
c52 d20
3.11 0.7908505 10 0.25008887
c52 d21
1.47 0.2110819 10 0.06674995
ddply(cabbage_exp, "Date" , transform,
percent_weight = Weight / sum(Weight) * 100 )
Cultivar Date Weight sd n se percent_weight
c39 d16 3.18 0.9566144 10 0.30250803 58.45588
c52 d16 2.26 0.4452215 10 0.14079141 41.54412
c39 d20 2.80 0.2788867 10 0.08819171 47.37733
c52 d20 3.11 0.7908505 10 0.25008887 52.62267
c39 d21 2.74 0.9834181 10 0.31098410 65.08314
c52 d21 1.47 0.2110819 10 0.06674995 34.91686
Once the percentages are computed, making the graph is the same as with a regular stacked bar
graph.
As with regular stacked bar graphs, it makes sense to reverse the legend order, change the color
palette, and add an outline. This is shown in ( Figure 3-21 ):
ggplot(ce, aes(x = Date, y = percent_weight, fill = Cultivar)) +
geom_bar(stat = "identity" , colour = "black" ) +
guides(fill = guide_legend(reverse = TRUE
TRUE )) +
scale_fill_brewer(palette = "Pastel1" )
Search WWH ::




Custom Search