# Stacked Bar Charts in R

January 10, 2013
By

(This article was first published on Mollie's Research Blog, and kindly contributed to R-bloggers)

### Reshape Wide to Long

Let’s use the Loblolly dataset from the datasets package. These data track the growth of some loblolly pine trees.

`> Loblolly[1:10,]   height age Seed1    4.51   3  30115  10.89   5  30129  28.72  10  30143  41.74  15  30157  52.70  20  30171  60.92  25  3012    4.55   3  30316  10.92   5  30330  29.07  10  30344  42.83  15  303`
First, we need to convert the data to wide form, so each age (i.e. 3, 5, 10, 15, 20, 25) will have its own variable.

`wide <- reshape(Loblolly, v.names = "height", timevar = "age", idvar = "Seed", direction = "wide")`
`> wide[1:5,]  Seed height.3 height.5 height.10 height.15 height.20 height.251  301     4.51    10.89     28.72     41.74     52.70     60.922  303     4.55    10.92     29.07     42.83     53.88     63.393  305     4.79    11.37     30.21     44.40     55.82     64.104  307     3.91     9.48     25.66     39.07     50.78     59.075  309     4.81    11.20     28.66     41.66     53.31     63.05`

### Create Variables

Then we want to create new columns showing how much each tree has grown between data points. For example, instead of knowing a tree’s height at age 10, we want to know how much it’s grown between age 5 and age 10, so that can be a bar in our graph.

`wide\$h0.3 <- wide\$height.3wide\$h3.5 <- wide\$height.5 - wide\$height.3wide\$h5.10 <- wide\$height.10 - wide\$height.5wide\$h10.15 <- wide\$height.15 - wide\$height.10wide\$h15.20 <- wide\$height.20 - wide\$height.15wide\$h20.25 <- wide\$height.25 - wide\$height.20`

### Plot Stacked Bar Chart

Finally, we want to plot all the new data points:

`library(RColorBrewer)sequential <- brewer.pal(6, "BuGn")`
`barplot(t(wide[,8:13]), names.arg = wide\$Seed, # x-axis labels cex.names = 0.7, # makes x-axis labels small enough to show all col = sequential, # colors xlab = "Seed Source", ylab = "Height, Feet", xlim = c(0,20), # these two lines allow space for the legend width = 1) # these two lines allow space for the legendlegend("bottomright",  legend = c("20-25", "15-20", "10-15", "5-10", "3-5", "0-3"), #in order from top to bottom fill = sequential[6:1], # 6:1 reorders so legend order matches graph title = "Years")`
 Stacked bar chart
If you decide you’d rather have clustered bars instead of stacked bars, you can just add the option beside = TRUE to the barplot.

The full code is available in a gist.

### Citations and Further Reading

To leave a comment for the author, please follow the link and comment on their blog: Mollie's Research Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

# Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts.(You will not see this message again.)

Click here to close (This popup will not appear again)