Friday, 6 April 2012

Dynamite plots in R

For some time I've contemplated creating a function for creating the dynamite plots beloved by many of the applied sciences. There's a lot of criticism regarding their utility, and there are several ways that present data in a more intelligible way. A search on the subject brings up pages with such emotive titles as "Dynamite plots: unmitigated evil?" and "Why dynamite plots are BAD". The "Beware of Dynamite" poster sums up the main problem with dynamite plots by concluding "Intentionally or not, a dynamite plot hides more than it reveals".

All that said, I'm an R advocate. If being able to to create dynamite plots is going to encourage people to use R, I can cope with their inadequacies. Here's hoping that the paucity of information required to create them makes people reconsider.
dynamitePlot <- function(height, error, names = NA,
        significance = NA, ylim = c(0,maxLim), ...){
    maxLim <- 1.1* max(mapply(sum, height, error))
    bp <- barplot(height, names.arg = names, ylim = ylim, ...)
    arrows(x0 = bp, y0 = height, y1 = height + error, angle = 90)
    text(x = bp, y = 0.2 + height + error, labels = significance)
}

Values <- c(1,2,5,4)
Errors <- c(0.25, 0.5, 0.33, 0.12)
Names <- paste("Trial", 1:4)
Sig <- c("a", "a", "b", "b")

dynamitePlot(Values, Errors, names = Names, significance = Sig)
The result looks like this:Dynamite plot The code is also available on gitHub.

4 comments:

Chris Beeley said...

Very nice! I've given up on dynamite plots after reading all the dire warnings (although I still harbour a secret liking for their simplicity) but I totally agree with you about making R friendly and useful being a good way of spreading the word.

George said...

If you want to encourage people to use R then show them how much easier it is to produce a boxplot than a dynamite plot and how much more informative it is.
Why code an inferior plot in several lines when the default R boxplot can be done in one?

George
http://sharpstatistics.co.uk/stats/comparing-data-sets/

Samuel Brown said...

Hi George

Fair point. However, I guess my philosophy is that it's easier for us to meet people at their level and then encourage them deeper, as opposed to expecting them to come up to ours.

The function can also be used to demonstrate the difference between the two plotting methods, eg:

aa <- rnorm(100, mean = 50, sd = 20)
bb <- rpois(100, lambda = 65)
cc <- rgamma(100, shape = 5, scale = 10)

heights <- sapply(list(aa, bb, cc), mean)
sds <- sapply(list(aa, bb, cc), sd)

dynamitePlot(heights, sds)
boxplot(aa, bb, cc)

Jon Sullivan said...

And here you were harassing me last week for doing a dynamite plot in R with an undergrad!

I accept the criticisms of dynamite plots but still like how they can quickly and intuitively convey the main trends. Boxplots contain more information, but they're ugly and need to be explained to people unfamiliar with them (e.g., most undergrads).