17

I'd like to produce an area/bar graph in R similar to this: Plot from David MacKay's book "Sustainable Energy" (plot from David MacKay's (excellent) book "Sustainable Energy")

I honestly can't even find the proper name for a plot like this. It seems to be a bar graph with variable width bars. Certainty a powerful communication tool.

3
  • Are you sure there isn't any amount of Adobe Illustrator work put into it? :) Commented Jan 29, 2013 at 20:05
  • 1
    this blog post might provide you with a solution. Commented Jan 29, 2013 at 20:10
  • 2
    @Arun, MacKay actually explains in the Acknowledgments that the figures were drawn with gnuplot and metapost. No Illustrator, no R. Hardcore. Commented Jan 29, 2013 at 20:48

2 Answers 2

27

You can do this with base graphics. First we specify some widths and heights:

widths = c(0.5, 0.5, 1/3,1/4,1/5, 3.5, 0.5)
heights = c(25, 10, 5,4.5,4,2,0.5)

Then we use the standard barplot command, but specify the space between blocks to be zero:

##Also specify colours
barplot(heights, widths, space=0, 
        col = colours()[1:6])

Since we specified widths, we need to specify the axis labels:

axis(1, 0:6)

To add grid lines, use the grid function:

##Look at ?grid to for more control over the grid lines
grid()

and you can add arrows and text manually:

arrows(1, 10, 1.2, 12, code=1)
text(1.2, 13, "A country") 

To add your square in the top right hand corner, use the polygon function:

polygon(c(4,4,5,5), c(20, 25, 25, 20), col="antiquewhite1")
text(4.3, 22.5, "Hi there", cex=0.6)

This all gives:

enter image description here


Aside: in the plot shown, I've used the par command to adjust a couple of aspects:

par(mar=c(3,3,2,1), 
    mgp=c(2,0.4,0), tck=-.01,
    cex.axis=0.9, las=1)
Sign up to request clarification or add additional context in comments.

1 Comment

@csgillespie amazing. If I didn't know better I'd think your plot was from the book. (But I am colorblind...)
18

Inspired by the code from the blog post I mentioned above,

df <- data.frame(x = c("Alpha", "Beta", "Gamma", "Delta"), width = c(25, 50, 75, 100), height = c(100, 75, 50, 25))
df$w <- cumsum(df$width)
df$wm <- df$w - df$width
df$wt <- with(df, wm + (w - wm)/2)

library(ggplot2)
p  <- ggplot(df, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))
library(grid) # needed for arrow function
p1 + geom_text(aes(x = wt, y = height * 0.8, label = x)) + 
     theme_bw() + labs(x = NULL, y = NULL) + 
     theme(axis.ticks = element_blank(),axis.text.x = element_blank(), 
     axis.text.y = element_blank(), legend.position = "none") + 
     annotate("text", x = 120, y = 83, label = "a Beta block") + 
     geom_segment(aes(x = 100, y = 80, xend = 80, yend = 75), 
     arrow = arrow(length = unit(0.5, "cm")))

beta blocker

2 Comments

(+1) The legends are kind of redundant here, aren't they?
The mentioned Blog post link, for the record. I'm glad there is a ggplot way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.