Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Last active May 5, 2017 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyLadson/62a10edad6901dbaef4b5540e5d50237 to your computer and use it in GitHub Desktop.
Save TonyLadson/62a10edad6901dbaef4b5540e5d50237 to your computer and use it in GitHub Desktop.
library(plotrix)
library(RColorBrewer)
###################
# The slopegraphs use the plotrix::bumpchart function
# Here I've tweaked plotrix::bumpchart to increase the spacing between the labels
#
# This just involves changing one line
#
# old: minspacing <- strheight("M") * 1.5
# new: minspacing <- strheight("M") * 2
body(bumpchart)[[17]] <- substitute( minspacing <- strheight("M") * 2)
# Figure 1
# Complaints
complaints <- matrix(c(
6.4, 5.0, 4.8, 4.0, 4.3, # Canberra
3.4, 3.5, 3.9, 3.2, 2.6, # Sydney
12.1, 9.5, 0.6, 1.0, 0.8, # Perth
53.1, 72.7, 37.5, 49.9, 39.5, # Darwin
4.8, 6.3, 7.4, 5.3, 4.0, # Melbourne
NA, 1.5, 2.4, NA, NA), # Adelaide
nrow = 6, byrow = TRUE)
complaints
cities <- c('Canberra', 'Sydney', 'Perth',
'Darwin', 'Melbourne',
'Adelaide')
rownames(complaints) <- cities
colnames(complaints) <- c('2010-11', '2011-12', '2012-13', '2013-14', '2014-15')
my_col = brewer.pal(6, name = 'Paired')
op <- par(oma = c(1, 8, 1, 1))
bumpchart(complaints, col = my_col,
rank = FALSE,
main = '')
axis(side = 2, at = c(0, 50), labels = c(0, 50), line = 1, outer = TRUE, las = 2, col = 'black')
mtext(side = 2, 'Total complaints per 1000 properties', outer = TRUE, line = 1.2, adj = 0.3)
par(xpd=TRUE)
boxed.labels(1.5, 1.5, labels = 'Adelaide', border = FALSE)
par(xpd=FALSE)
par(op)
# Figure 2 Capex
capex <- matrix(c(
700348, 550203, 504668, 262529, 351732, # Perth
1355928, 942090, 735799, 609752, 686136, # Melbourne
805693, 792228, 713762, 629305, 641685, # Sydney
504764, 989579, 653563, 517648, 487636, # South East Queensland
256668, 230838, 143365, 59393, 48937, # Canberra
595851, 530075, 331038, 187945, 152124, # Adelaide
51978, 58080, 64789, 25600, NA), # Darwin
nrow = 7, byrow = TRUE)
cities <- c('Perth', 'Melbourne', 'Sydney','SE Queensland', 'Canberra', 'Adelaide', 'Darwin')
rownames(capex) <- cities
colnames(capex) <- c('2010-11', '2011-12', '2012-13', '2013-14', '2014-15')
my_col = brewer.pal(7, name = 'Paired')
op <- par(oma = c(1, 1, 1, 8))
bumpchart(capex, col = my_col,
rank = FALSE,
main = '')
axis(side = 4, at = c(0, 1000000), labels = c('$0', '$1 million'), line = 1, outer = TRUE, las = 2)
par(op)
# Figure 3: Include total
capex <- rbind(capex, apply(capex, 2, sum, na.rm = TRUE))
rownames(capex)[8] <- 'Total'
colnames(capex) <- c('2010-11', '2011-12', '2012-13', '2013-14', '2014-15')
my_col = brewer.pal(9, name = 'Paired')
op <- par(oma = c(1, 1, 1, 8))
bumpchart(capex, col = my_col,
rank = FALSE,
main = '')
axis(side = 4, at = c(0, 1000000, 2000000, 3e+6, 4e+6),
labels = c('$0', '$1 million', '$2 million','$3 million', '$4 million'),
line = 1, outer = TRUE, las = 2)
par(op)
# Using Thomas Leepers Slopegraph
# https://github.com/leeper/slopegraph
# This isn't working quite right at present but could
# probably be sorted out
capex <- matrix(c(
700348, 550203, 504668, 262529, 351732, # Perth
1355928, 942090, 735799, 609752, 686136, # Melbourne
805693, 792228, 713762, 629305, 641685, # Sydney
504764, 989579, 653563, 517648, 487636, # South East Queensland
256668, 230838, 143365, 59393, 48937, # Canberra
595851, 530075, 331038, 187945, 152124, # Adelaide
51978, 58080, 64789, 25600, NA), # Darwin
nrow = 7, byrow = TRUE)
capex <- capex/1000
capex <- data.frame(capex)
cities <- c('Perth', 'Melbourne', 'Sydney','SE Queensland', 'Canberra', 'Adelaide', 'Darwin')
row.names(capex) <- cities
names(capex) <- c('X2010-11', 'X2011-12', 'X2012-13', 'X2013-14', 'X2014-15')
my_col = brewer.pal(9, name = 'Paired')
par(oma = c(1,4,1,1))
slopegraph(capex,
labels = c('2010-11', '2011-12', '2012-13', '2013-14', '2014-15'),
decimals = 0,
col.lines = my_col,
col.lab = my_col,
col.num = my_col,
family = 'sans')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment