MyNixOS website logo
Description

Bump Chart and Sigmoid Curves.

A geom for ggplot to create bump plots. Can be good to use for showing rank over time.

ggbump

The R package ggbump creates elegant bump charts in ggplot.Bump charts are good to use to plot ranking over time.

Installation

You can install the released version of ggbump from github with:

devtools::install_github("davidsjoberg/ggbump")

Bump chart Examples

Basic example:

A more advanced example:

Example2

Click here for code to the plot above

Flags could be used instead of names:

Example3

Click here for code to the plot above

With geom_sigmoid you can make custom sigmoid curves:

Example4

Click here for code to the plot above

Tutorial

Prep

Load packages and get some data with rank:

if(!require(pacman)) install.packages("pacman")
library(ggbump)
pacman::p_load(tidyverse, cowplot, wesanderson)

df <- tibble(country = c("India", "India", "India", "Sweden", "Sweden", "Sweden", "Germany", "Germany", "Germany", "Finland", "Finland", "Finland"),
             year = c(2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013),
             rank = c(4, 2, 2, 3, 1, 4, 2, 3, 1, 1, 4, 3))

knitr::kable(head(df))
countryyearrank
India20114
India20122
India20132
Sweden20113
Sweden20121
Sweden20134

Make a bump chart

Most simple use case:

ggplot(df, aes(year, rank, color = country)) +
    geom_bump()

Pimp the bump chart!

Improve the bump chart by adding:

  • A point for each rank observation.
  • Choose a minimal theme, I use theme_minimal_grid() from cowplot.
  • Choose nice colors so it does not look generic ggplot. I use a palette from wesanderson.
  • Remove legend and add labels at the start and end of the bumpy ride.
  • Reverse the y-axis to get rank 1 at the top.
  • Adjust the ‘smoothness’ of the lines by setting smooth to 8. Higher means less smooth.

ggplot(df, aes(year, rank, color = country)) +
  geom_point(size = 7) +
  geom_text(data = df %>% filter(year == min(year)),
            aes(x = year - .1, label = country), size = 5, hjust = 1) +
  geom_text(data = df %>% filter(year == max(year)),
            aes(x = year + .1, label = country), size = 5, hjust = 0) +
  geom_bump(size = 2, smooth = 8) +
  scale_x_continuous(limits = c(2010.6, 2013.4),
                     breaks = seq(2011, 2013, 1)) +
  theme_minimal_grid(font_size = 14, line_size = 0) +
  theme(legend.position = "none",
        panel.grid.major = element_blank()) +
  labs(y = "RANK",
       x = NULL) +
  scale_y_reverse() +
  scale_color_manual(values = wes_palette(n = 4, name = "GrandBudapest1"))

geom_bump with factors

To use geom_bump with factors or character axis you need to prepare the data frame before. You need to prepare one column for the numeric position and one column with the name. If you want to have character/factor on both y and x you need to prepare 4 columns.

# Original df
df <- tibble(season = c("Spring", "Summer", "Autumn", "Winter", 
                        "Spring", "Summer", "Autumn", "Winter", 
                        "Spring", "Summer", "Autumn", "Winter"),
             position = c("Gold", "Gold", "Bronze", "Gold",
                          "Silver", "Bronze", "Gold", "Silver",
                          "Bronze", "Silver", "Silver", "Bronze"),
             player = c(rep("David", 4),
                        rep("Anna", 4),
                        rep("Franz", 4)))

# Create factors and numeric columns
df <- df %>% 
  mutate(season = factor(season, 
                         levels = c("Spring", "Summer", "Autumn", "Winter")),
         x = as.numeric(season),
         position = factor(position, 
                           levels = c("Gold", "Silver", "Bronze")),
         y = as.numeric(position))

# Add manual axis labels to plot
p <- ggplot(df, aes(x, y, color = player)) +
  geom_bump(size = 2, smooth = 8, show.legend = F) +
  geom_point(size = 5, aes(shape = player)) +
  scale_x_continuous(breaks = df$x %>% unique(),
                     labels = df$season %>% levels()) +
  scale_y_reverse(breaks = df$y %>% unique(),
                     labels = df$position %>% levels())
p

And some nice theme features

p +
  theme_minimal_grid(font_size = 14, line_size = 0) +
  theme(panel.grid.major = element_blank(),
        axis.ticks = element_blank()) +
  labs(y = "Medal",
       x = "Season",
       color = NULL,
       shape = NULL) +
  scale_color_manual(values = wes_palette(n = 3, name = "IsleofDogs1"))

Feedback

If you find any error or have suggestions for improvements you are more than welcome to contact me :)

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows