Introduction to the mekko package

Bar mekko graphics in R

This is a quick introduction to the bar mekko function* in the mekko package.

Its main value is to add quantitative context to a bar graph, via bar width.

knitr::opts_chunk$set(
  fig.width = 7,
  fig.height = 4
)


Install with:

install.packages("mekko")

You can also install the development version with devtools::install_github('ryninho/mekko', build_vignettes = TRUE).

View examples with:

vignette("mekko-vignette")

First we'll load ggplot and mekko and create some example data.

library(ggplot2)
library(mekko)
profits <- data.frame(
  product = c('widgets', 'sprockets', 'cogs', 'whosits', 'whatsits'),
  profit_margin = c(3.2, -1.4, 0.1, -5.5, 11.9),
  revenue = c(850, 1600, 900, 675, 250)
  )

Bar mekko

Now let's take a look at profit margin by product using ggplot2.

ggplot(profits, aes(x = product, y = profit_margin)) +
  geom_bar(stat = "identity")

Well that's insightful, but I don't know how worried I should be about the margin on whosits or cogs, nor do I know how happy I should be about whatsits knocking it out of the park. Maybe I can add revenue as the bar width so I know what's important here?

ggplot(profits, aes(x = product, y = profit_margin, width = revenue)) +
  geom_bar(stat = "identity") +
  labs(title = "Variable bar width fail :(")

Well shucks, that looks like some kind of Atari graphics airplane. Let's use the mekko package to put our margins in context.

bmx <- barmekko(profits, product, profit_margin, revenue)
bmx

Alright, so actually the weak profit margins on sprockets may be worth nearly as much focus as the problem with whosits. Also, no high-fives for margins on the whatsits until we triple sales of them.

Those labels are a little close together- this is a ggplot object so let's use the usual method of rotating the axes.

bmx + theme(axis.text.x = element_text(angle = 90, hjust = 1))

Appendix



Try the mekko package in your browser

Any scripts or data that you put into this service are public.

mekko documentation built on Jan. 3, 2018, 10:34 p.m.