7
$\begingroup$

I try to reproduce a plot which I found in a publication (Mayor-Fernández et al 2012) showing unemployment rates across time for several regions. It combines a heatmap (?), a time series plot (lower panel) and boxplots (right panel).

For a better impression of the plot please see page 7 in http://www2.dse.unibo.it/wp/WP835.pdf

Can someone guide me in a correct direction or share some code? I appreciate your help and time very much!

$\endgroup$
  • 1
    $\begingroup$ Welcome to the site, @Marc. FWIW, this question probably belonged on Stack Overflow. This is a gray area, but I would say that Q's focusing on a data visualization (eg, nature, quality, etc) go here, & Q's on how to make a data vis (eg, in R) are better on SO. Since you've already got a great answer, I see no harm in letting it stay; just FYI. $\endgroup$ – gung - Reinstate Monica Aug 10 '12 at 16:05
14
$\begingroup$

You can make each of the plots easily enough. Sticking with your example, I'll use unemployment data from the European countries between 1999 and 2011 (from Eurostat), called unempd (sorry it's long!):

> dput(unempd)
structure(list(Year = c(1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2000L, 2001L, 
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 
2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 
2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
2011L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 
2010L, 2011L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 
2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 
2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 
2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2011L, 1999L, 2000L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 1999L, 
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2011L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2011L), Country = structure(c(2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 
27L, 27L, 27L, 27L, 27L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 26L, 26L, 26L, 26L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Austria", 
"Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", 
"Denmark", "Estonia", "Finland", "France", "Germany", "Greece", 
"Hungary", "Iceland", "Ireland", "Italy", "Latvia", "Lithuania", 
"Luxembourg", "Malta", "Netherlands", "Norway", "Poland", "Portugal", 
"Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", 
"United Kingdom"), class = "factor"), Unemployment = c(8.6, 7, 
6.6, 7.5, 8.2, 8.4, 8.4, 8.2, 7.5, 7, 7.9, 8.3, 7.1, 3.6, 4.1, 
18.2, 13.7, 12, 10.1, 9, 6.9, 5.6, 6.8, 10.2, 11.2, 8.8, 8.8, 
8.2, 7.3, 7.8, 8.3, 7.9, 7.1, 5.3, 4.4, 6.7, 7.3, 6.7, 5.6, 4.6, 
4.6, 4.6, 5.4, 5.5, 4.8, 3.9, 3.8, 3.3, 6, 7.4, 7.6, 8.9, 7.9, 
7.8, 8.5, 9.8, 10.7, 11.1, 10.2, 8.6, 7.5, 7.7, 7.1, 5.9, 11.6, 
13.6, 12.6, 10.3, 10, 9.7, 7.9, 5.9, 4.7, 5.5, 13.8, 16.9, 12.5, 
5.8, 4.3, 3.9, 4.4, 4.7, 4.5, 4.3, 4.4, 4.6, 6, 11.7, 13.5, 14.4, 
12.1, 11.4, 10.8, 10.3, 9.7, 10.5, 9.8, 8.9, 8.3, 7.7, 9.5, 12.5, 
17.7, 15.7, 13.9, 10.5, 11.5, 11.5, 11, 9.2, 8.5, 8.3, 11.3, 
18, 20.1, 21.6, 12, 10.2, 9.1, 9.2, 8.9, 9.3, 9.3, 9.3, 8.4, 
7.8, 9.5, 9.7, 9.7, 11.4, 10.6, 9.5, 9, 8.7, 8, 7.7, 6.8, 6.1, 
6.7, 7.8, 8.4, 8.4, 5, 4, 3.3, 4.1, 4.3, 5.3, 4.5, 3.9, 3.7, 
5.3, 6.2, 7.7, 13.8, 14.2, 13.1, 12.1, 10.5, 10.4, 8.9, 6.8, 
6, 7.5, 17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, 2.7, 2.5, 
2.9, 4.1, 4.3, 4.4, 4, 3.7, 3.3, 4.1, 4.5, 4.1, 15.1, 13.9, 13.7, 
12.6, 11.1, 9.6, 8.4, 9.1, 11.8, 13.4)), .Names = c("Year", "Country", 
"Unemployment"), class = "data.frame", row.names = c(NA, -397L
))

You can make the heatmap with:

library(ggplot2)
hmplot <- ggplot(unempd, aes(Year, Country, fill=Unemployment))
hmplot + geom_tile(colour="white") + scale_fill_gradient(low="light blue", high="dark blue") + 
  ylab("") + xlab("") + opts(legend.position="none")

which produces the following plot:

Heatmap plot

Then to make the time series plot, you can use geom_line(stat="identity") [I just averaged the yearly figures from countries using the ddply function from the plyr package which obviously isn't a legitimate reflection of unemployment rate across Europe, but hopefully works for the sake of illustration...].

library(plyr)
unempxyr <- ddply(unempd, .(Year), summarise, meanunemp = mean(Unemployment))
tsplot <- ggplot(unempxyr, aes(Year, meanunemp))
tsplot + geom_line(stat="identity") + ylab("Level") + xlab("") + 
  scale_y_continuous(lim=c(5,10)) + theme_bw() 

This results in this graphic:

Time series plot

Finally, for the "boxplots", I again used ddply to calculate the boxplot statistics for each country:

countryxemp <- ddply(unempd, .(Country), summarise, 
  minemp = fivenum(Unemployment)[1],
  q2emp = fivenum(Unemployment)[2],
  medemp = fivenum(Unemployment)[3],
  q3emp = fivenum(Unemployment)[4],
  maxemp = fivenum(Unemployment)[5]
)
bplot <- ggplot(countryxemp, aes(medemp, Country)) + geom_point()
bplot + geom_errorbarh(aes(xmin=minemp, xmax=q2emp), colour=I("black"), height=0) +
  geom_errorbarh(aes(xmin=q3emp, xmax=maxemp), colour=I("black"), height=0) +
  ylab("") + xlab("Levels\n (internal)") + theme_bw()

which results in this graphic:

Boxplots

Is this close enough to what you want? Putting the plots together in the way the article does is another matter. I'm not sure if it's possible via gridExtra::grid.arrange() or something similar to that...?

$\endgroup$
  • 1
    $\begingroup$ Hi smillig, sorry for my late response. this is not close enough, this is exactly what i was looking for! thank you so much, I appreciate your helping hand! best marc $\endgroup$ – Peter Aug 13 '12 at 10:14
4
$\begingroup$

I know your answer was marked for R, but if you're open to an Excel solution, its relatively easy to work up the same sort of graphic:

ExcelHeatMap

From my perspective, Excel has a couple of advantages over R. First more people have access to it then can use R (I think I'm a prime example of that), so you have more audience reach with the software. Also, in Excel this is an interactive chart, so the top row (Netherlands in this example) can be used as a selector from any of the series and when changed repopulates all the related areas in that row, plus the target series in the lower charts.

$\endgroup$
  • 3
    $\begingroup$ Would the down-voter care to give me some constructive guidance on how to improve my answer please? $\endgroup$ – dav Apr 4 '13 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.