Data visualization in R: Clustered and stacked bar plot

A bar plot can be used to show data in a cluster or stacked by category. Within this article I want to show you both cases.

 
Clustered bar plot

By default a bar plot is clustered. The following example creates a clustered bar plot with the monthly sales data of books, magazines and newspapers.

months = c(’01/2014′, ’02/2014′, ’03/2014′)

sales = data.frame(
books = c(15,17,11),
magazines = c(8,8,9),
newspapers = c(11,10,9))

sales <- do.call(rbind,sales)

par(mar = c(5,4,4,6))
barplot(sales, beside=TRUE, ylim=c(0,25), legend.text = rownames(sales),
args.legend = list(x = „topleft“, bty=“n“), names.arg = months)

R30_a

Stacked bar plot

The same data can be shown by creating stacked bars. This will allow you to see the sum of values in each category. To create a stacked bar plot you have to remove the beside parameter. Furthermore I have increased the limits of the y-axis and moved the legend outside of the plot.

months = c(’01/2014′, ’02/2014′, ’03/2014′)

sales = data.frame(
books = c(15,17,11),
magazines = c(8,8,9),
newspapers = c(11,10,9))

sales <- do.call(rbind,sales)

par(mar = c(5,4,4,8))
barplot(sales, ylim=c(0,40),
legend.text = rownames(sales),
args.legend = list(x = ‚right‘, bty=’n‘, inset=c(-0.50,0), xpd = TRUE),
names.arg = months)

R30_b

Werbung
Dieser Beitrag wurde unter R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s