Giter Site home page Giter Site logo

stickylabeller's Introduction

stickylabeller

The stickylabeller package helps you label the facets in your ggplot2 plots. If you know how to use the glue package, you know how to use stickylabeller!

Installation

Install stickylabeller from GitHub using devtools:

devtools::install_github("rensa/stickylabeller")

Use

The package has just one function: label_glue. Give it a string template to be processed by glue, and it'll return a labelling function that you can pass to facet_*:

library(stickylabeller)

# here's some example data
mydf = data_frame(
  x = 1:90,
  y = rnorm(90),
  red = rep(letters[1:3], 30),
  blue = c(rep(1, 30), rep(2, 30), rep(3, 30)))

# and here's a plot!
ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    ~ red + blue,
    labeller = label_glue('Red is {red}\nand blue is {blue}'))

facet_wrap labelled with two facet column values

ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_grid(
    red ~ blue,
    labeller = label_glue(
      rows = 'Red is {red}',
      cols = 'Blue is {blue}'))

facet_grid labelled with one facet column value on each margin

Your label_glue labeller can refer to any of the data frame columns included in the facetting formula. It can also use those columns in expressions, like:

label_glue('Red is {toupper(red)}\nand blue is {blue}')

Numbering sequential facets

As well as the columns you include in the facetting specification, stickylabeller includes a few helper columns:

  • .n numbers the facets numerically: "1", "2", "3"...
  • .l numbers the facets using lowercase letters: "a", "b", "c"...
  • .L numbers the facets using uppercase letters: "A", "B", "C"...
  • .r numbers the facets using lowercase Roman numerals: "i", "ii", "iii"...
  • .R numbers the facets using uppercase Roman numerals: "I", "II", "III"...

So you can automatically number your facets like:

ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    ~ red + blue,
    labeller = label_glue('({.l}) Red is {toupper(red)}\nand blue is {blue}'))

facet_wrap labelled with two facet column values and numbered (a), (b), (c), ...

Limitations

  • .n, .l and .L only work with facet_wrap for now. See Issue #1.

Including summary statistics in facet labels

There are a couple of ways to include summary statistics using stickylabeller. The most flexible way (but probably not the most performant, if you're working with a massive dataset) is to summarise your data and join it back to the original data, so that the summary statistics appear as new columns in the original data. Then include the summary columns in your facetting specification:

library(dplyr)

# summarise the data
multi_summary = mydf %>%
  group_by(red, blue) %>%
  summarise(
    mean_y = sprintf('%#.2f', mean(y)),
    sd_y = sprintf('%#.2f', sd(y))) %>%
  ungroup()

# join it back to the original data
mydf = mydf %>%
  inner_join(multi_summary)

# plot! remember to include the summaries in your facetting spec
ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    ~ red + blue + mean_y + sd_y,
    labeller = label_glue(
      '({.L}) Red = {red}, blue = {blue}\n(mean = {mean_y}, SD = {sd_y})'))

facet_wrap labelled with two facet column values and two summary statistics, each numbered (a), (b), (c), ...

This works even if you're facetting by multiple columns and summarising by multiple columns. Keep in mind, however, that if you're going to continue to work with the data after plotting, you might want to drop the summary columns in order to avoid confusing yourself.

An alternate way to accomplish this is to convert each of your summary statistics into a vector named for the values of your facet column. This gets really messy with more than one facet column, though!

Have fun! If you hit any snags, please feel free to file an issue here so that I can get on it! <3

stickylabeller's People

Contributors

jimjam-slam avatar hughjonesd avatar

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.