GithubHelp home page GithubHelp logo

slopegraph's Introduction

Tufte-Inspired Slopegraphs in R

This repository holds some working code for creating "slopegraphs" in R.

This is very much a work in progress. Once it's more stable, I will release the package to CRAN.

Pull requests welcome. Please report any issues on the issues page.

The package currently includes one mainfunction, slopegraph(), which produces a slopegraph from an observation-by-period data frame. Everything is more or less drawn automatically, but is highly customizable in terms of line and text colors, font sizes and styles, axes, titles, and plotting behind and in front of the slopegraph lines. An underlying function, segmentize() produces the data structure used for the actual plotting. And a new function, ggslopegraph() does the same as slopegraph() but using ggplot2 graphics.

Examples

The current output of the slopegraph() function (for the examples included in documentation) are shown below.

Tufte's most famous slopegraph example is probably the "cancer survival graph," depicting 5, 10, 15, and 20 year survival rates for various cancers. The first example mimics this result but draws it to the correct scale (unlike Tufte's original):

library("slopegraph")
data(cancer)
slopegraph(cancer, col.lines = 'gray', col.lab = "black", 
           xlim = c(-.5,5.5), cex.lab = 0.5, cex.num = 0.5,
           xlabels = c('5 Year','10 Year','15 Year','20 Year'))

Cancer Survival

The second example, also from Tufte, shows changes in gross domestic product for a small set of countries over two points in time:

data(gdp)
slopegraph(gdp, col.lines = 'gray', col.lab = "black", xlabels = c('1970','1979'),  
           main = 'Current Receipts of Goverment as a Percentage of Gross Domestic Product')

GDP

This third example comes from an 1878 publication (a copy of which is available here), showing the relative ranking of the population of various U.S. states. This example features a reversed y-axis to better display the ranking and I demonstrate the col.lines argument to highlight South Carolina:

data(states)
cols <- `[<-`(rep("black", 37), 7, "red")
slopegraph(states, xlim = c(-1, 12), ylim = c(37,0), offset.x = 0.06,
           col.lines = cols, col.lab = cols, 
           main = 'Relative Rank of U.S. State Populations, 1790-1870')

states

As of v0.1.9, there is also a ggplot2-based function, ggslopegraph() that produces a similar representation but using ggplot2 graphics:

require("ggplot2")
## Loading required package: ggplot2
data(states)
cols <- `[<-`(rep("black", 37), 7, "red")
ggslopegraph(states, offset.x = 0.06, yrev = TRUE,
  col.lines = cols, col.lab = cols, 
  main = 'Relative Rank of U.S. State Populations, 1790-1870') +
 theme_bw()    
## Warning: Removed 84 rows containing missing values (geom_text).

ggstates

Installation

CRAN Build Status Build status codecov.io Project Status: Work in Progress

To install the latest development version of slopegraph from GitHub:

if (!require("ghit")) {
    install.packages("ghit")
}
ghit::install_github("leeper/slopegraph")

slopegraph's People

Contributors

geekonacid avatar leeper avatar martindaniel4 avatar pbradl42 avatar

Watchers

 avatar  avatar  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.