Stacked Barplots USPersonalExpenditure
Note: This article shows an R example of simple stacked and proportions stacked barplots. For more general information, please refer to the following entries: Stacked Barplots and Barplots, Histograms and Boxplots.
In short: In this article, the dataset USExpenditures is visualised using simple and proportions stacked barplots.
Contents
The Dataset[edit]
The dataset contains personal expenditures of US citizens (in billions of dollars) in the categories food and tobacco, household operation, medical and health, personal care, and private education for the years 1940, 1945, 1950, 1955 and 1960.
Examining the Dataset[edit]
Therefore, it has five columns for the years and five rows for the categories, which can be inspected using View or head.
USexp<-USPersonalExpenditure head(USexp)
Output[edit]
> head(USexp)
1940 1945 1950 1955 1960
Food and Tobacco 22.200 44.500 59.60 73.2 86.80
Household Operation 10.500 15.500 29.00 36.5 46.20
Medical and Health 3.530 5.760 9.71 14.0 21.10
Personal Care 1.040 1.980 2.45 3.4 5.40
Private Education 0.341 0.974 1.80 2.6 3.64
Simple stacked barplot[edit]
In order to create aesthetically appealing visualisations, you can resort to packages that include nice colour combinations. Here, we are going to use the Wes Anderson package, which has to be installed first.
install.packages("wesanderson")
library("wesanderson")
In this example, the margin around the graphic on the right is extended to fit the legend. Then the stacked barplot is created and subsequently a legend is added.
par(mar = c(5.1, 4.1, 4.1, 10), xpd = TRUE) #extend margin
barplot(USexp,
col = wes_palette("Darjeeling2"),
border = "white",
space = 0.04,
font.axis = 2,
xlab = "group",
ylim = c(0, 200))
#create legend on the right side of the plot
legend("topright", inset = c(-0.33, 0),
legend = rownames(USexp),
fill = wes_palette("Darjeeling2"),
border = "white",
bty = "n")
par(mar = c(5.1, 4.1, 4.1, 2.1)) #reset margin
Output[edit]
Total personal expenditure rose over the 20 years displayed here. To see the proportions of the amount spent on the different categories and whether they changed, we can create a percentage plot.
Proportions stacked barplot[edit]
USexp_percentage <- apply(USexp, 2, function(x){ x * 100 / sum(x, na.rm = T) })
par(mar = c(5.1, 4.1, 4.1, 10), xpd = TRUE) #extend margin
percplot <- barplot(USexp_percentage, col =wes_palette("Moonrise3"), #trying another colour palette
border = "white", xlab = "group")
# add percentage values
text(x = rep(percplot, each = nrow(USexp_percentage)),
y = apply(USexp_percentage, 2, cumsum) - USexp_percentage / 2,
labels = paste0(round(USexp_percentage, 1), "%"),
cex = 0.7,
col = "black")
legend("topright", inset = c(-0.30, 0),
legend = rownames(USexp),
fill = wes_palette("Moonrise3"),
border = "white",
bty = "n")
par(mar = c(5.1, 4.1, 4.1, 2.1)) #reset margin
Output[edit]
Here we can see that the proportion spent on the different categories roughly stayed the same over the years.
Further Resources[edit]
Wes Anderson colour palette: https://github.com/karthik/wesanderson
Grouped, stacked and percent stacked barplot in base R: https://r-graph-gallery.com/211-basic-grouped-or-stacked-barplot.html
The author of this entry is Antonia Ucher