Created
November 14, 2016 21:22
-
-
Save baptiste/abb56e4ce28d5fd380d3fe4878b15ac2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(scales) | |
library(dplyr) | |
# http://www.stuff.co.nz/national/86458731/Cheviot-earthquake-Tracing-the-source-of-the-7-5-magnitude-quake-and-its-aftermath?utm_source=dlvr.it&utm_medium=twitter | |
eqs <- data.frame( | |
energy = c(48.99, 13.34, 2.67, 6.25, 4.93, 2.71, 4.73, 16.38), | |
period = c("Nov 14 2016", "Earlier 2016", paste("All", 2015:2010)), | |
stringsAsFactors = FALSE) | |
eqs <- eqs %>% | |
mutate(period = factor(period, levels = period)) %>% | |
arrange(desc(period)) %>% | |
mutate(cumenergy = cumsum(energy), | |
centres = cumenergy - energy / 2) | |
leg <- guide_legend(reverse = TRUE) | |
library(grid) | |
stextGrob <- function (label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), r=0.03, colour="black", | |
just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE, | |
default.units = "npc", name = NULL, gp = gpar(), vp = NULL){ | |
let <- textGrob("a", gp=gp, vp=vp) | |
wlet <- grobWidth(let) | |
hlet <- grobHeight(let) | |
tg <- textGrob(label=label, x=x, y=y, gp=gpar(col=colour), | |
just = just, hjust = hjust, vjust = vjust, rot = rot, | |
check.overlap = check.overlap, | |
default.units = default.units) | |
tgl <- c(lapply(seq(0, 2*pi, length=36), function(theta){ | |
textGrob(label=label,x=unit(x, "native")+cos(theta)*r*wlet, | |
y=unit(y, "native")+sin(theta)*r*hlet, gp=gpar(col="white"), | |
just = just, hjust = hjust, vjust = vjust, rot = rot, | |
check.overlap = check.overlap, | |
default.units = default.units) | |
}), list(tg)) | |
g <- gTree(children=do.call(gList, tgl), vp=vp, name=name, gp=gp) | |
} | |
grid.stext <- function(...){ | |
g <- stextGrob(...) | |
grid.draw(g) | |
invisible(g) | |
} | |
GeomStext <- ggproto("GeomStext", Geom, | |
required_aes = c("x", "y", "label"), | |
default_aes = aes(size = 5), | |
draw_key = function (data, params, size) | |
{ | |
stextGrob(label=data$label, data$x, data$y, size=data$size, colour=data$colour) | |
}, | |
draw_group = function(data, panel_scales, coord) { | |
coords <- coord$transform(data, panel_scales) | |
stextGrob(label=coords$label, coords$x, coords$y, colour=coords$colour) | |
} | |
) | |
geom_stext <- function(mapping = NULL, data = NULL, stat = "identity", | |
position = "identity", na.rm = FALSE, show.legend = NA, | |
inherit.aes = TRUE, ...) { | |
layer( | |
geom = GeomStext, mapping = mapping, data = data, stat = stat, | |
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
params = list(na.rm = na.rm, ...) | |
) | |
} | |
ggplot(eqs, aes(x = 1, weight = energy, fill = period)) + | |
geom_bar(width = 1, colour = "grey50") + | |
geom_stext(x = 1.3, aes(y = centres, colour=period, label = paste0(energy, "%"))) + | |
coord_polar(theta = "y") + | |
scale_fill_brewer(palette = "Oranges", direction = -1, guide = leg) + | |
scale_color_brewer(palette = "Blues", direction = 1, guide = leg) + | |
theme_minimal(base_family = "Arial") + | |
theme(axis.ticks = element_blank(), | |
axis.text = element_blank(), | |
axis.title = element_blank(), | |
plot.caption = element_text(hjust = 0.5)) + | |
labs(fill = "", colour = "", | |
caption = "Source: http://www.stuff.co.nz/national/86458731/\nCheviot-earthquake-Tracing-the-source-of-the-7-5-magnitude-quake-and-its-aftermath\nSupplied by John Holdaway") + | |
ggtitle("Half the earthquake energy released since 2010 came in a single day", | |
subtitle = "Energy released in all New Zealand earthquakes, 2010-2016") |
Author
baptiste
commented
Nov 14, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment