Coder Social home page Coder Social logo

mekko's Introduction

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.


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 :(")
## Warning: position_stack requires non-overlapping x intervals

## Warning: position_stack requires non-overlapping x intervals

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

  • This package originally included a marimekko function as well, but the latter was discovered to be well covered in other packages, so it was removed to simplify maintenance
  • While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph

mekko's People

Contributors

ryninho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jrnold julius-v

mekko's Issues

Add geom_barmekko() function and GeomBarMekko ggproto object

Hi, I talked to you after the BARUG last night. After the presentation, I looked through your code and you could definitely write a geom_barmekko function that returns a geom and would allow you to use in facets, with groups, and all that goodness.

Take a look at the source for geom_bar. The function is just a wrapper to create and return a ggproto object for the geom. The heavy lifting is done by that object, and requires defining function to transform the data and calculate various things. https://ggplot2.tidyverse.org/articles/extending-ggplot2.html covers the internals of how ggplot works. Also this help page for the different types of ggproto objects (Geom, Stat, etc.) is useful.

The ggproto object could use GeomBar as a template and inherit from GeomRect. I think the only difference between GeomBar and GeomBarMekko would be to rewrite the setup_data() method to use positions() to calculate the widths and also calculate the correct xmin and xmax locations. Then remove draw_panel() since you don't need the "hack".

I've written a few geoms, so I'd be happy to answer any questions. I could also give it a go and open a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.