Javier
Javier

Reputation: 332

How would you replicate this graphic in R

I'm wondering how to do this graph in R?

graph

I tought about using geom_bar from the ggplot package. However I don't know how to plot the changing thickness and the colors that reflect the growth for the years at the same time.

I'd appreciate any ideas.

Thank you.

Upvotes: 2

Views: 211

Answers (1)

rawr
rawr

Reputation: 20811

Here are some steps to start. Nothing special, just some rectangles. There is a lot of information going on in this chart, most of it is in the text along the sides, usually meaning that the chart isnt very effective.

The only useful info the chart really shows is the yearly change in colors. You can do something like that below where I colored based on the rectangle height.

But +1 for finding another way to visualize your data.

enter image description here

set.seed(1)
nr <- 4
nc <- 50
mm <- matrix(sort(runif(nr * nc)) * 10, nr, nc)
nn <- matrix(sort(runif(nr * nc), decreasing = TRUE) * 10, nr, nc)
mm <- do.call('rbind', l <- list(mm, nn))[order(sequence(sapply(l, nrow))), ]

yy <- 50
mm <- rbind(mm, yy - colSums(mm))
nr <- nrow(mm)

plot(0:nc, type = 'n', ylim = c(0, yy), bty = 'n', axes = FALSE, ann = FALSE)
rect(s <- sequence(nc), 0, s + .95, mm[1, ], border = NA,
     col = as.numeric(cut(mm[1, ], breaks = seq(0, 15, 3))))
axis(1, s + .5, labels = s + 2000, lwd = 0, lwd.ticks = 1)
axis(1, s, labels = NA, lwd = 0, lwd.ticks = .5, tcl = -.2)
# axis(2, las = 1, lwd = 0)
mtext('Share of private jobs', side = 2, at = par('usr')[3], adj = 0)
arrows(.5, 0, .5, yy, lwd = 2, xpd = NA, length = .1)
text(par('usr')[1:2] + c(.5, -.5), yy, labels = range(s) + 2000,
     xpd = NA, pos = 3, adj = 0)

yy <- matrix(0, 1, nc)
for (ii in 2:nr) {
  yy <- colSums(rbind(yy, mm[ii - 1, ]))
  rect(s, yy  + 1, s + .95, yy + mm[ii, ], border = NA, 
       col = as.numeric(cut(mm[ii, ], breaks = seq(0, 15, 3))))
}

(I hope no one submits this to junk charts :{ )

Also here is another incredibly simple way. Should have done this first, ie, create sample data, pad the rows, and let barplot take care of the rest. A lot less control over this approach, though.

mm <- matrix(sort(runif(10)), 2) * 10
nn <- matrix(.5, 2, ncol(mm))
mm <- do.call('rbind', l <- list(mm, nn))[order(sequence(sapply(l, nrow))), ]
yy <- 22
mm <- rbind(mm, yy - colSums(mm))
barplot(mm, col = 1:0, border = NA, axes = FALSE)

enter image description here

Upvotes: 8

Related Questions