while starting with analytics almost everyone is first introduced to Ggplot in R. Althought ggplot is an accurate and convenient plotting tool, we feel that it lacks the wow-factor and the charts may seem dull. so, we move on to other plotting tools such as plotly and tableau that offer more visual flair. I belonged to the same category untill i came across the harvards tutorial about ggplot http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html.

Ggplot is a much underrated complex beast that we often overlook. This post gives you a deep dive into its capability to make you think twice before you opt to use the non-free tools such as tableau. so, to uncloak the capability of ggplot, i have decided to recreate the some graphs from magazine publications. As I am an ardent fan of economist articles (if you havent heard of economist then you should definitly check it out), i decided to recreate two most recent Econimist’s graphs on the ongoing climate crises.

here are the two graphs that I decide to redo with ggplot Temperature increase predictions CO2 emissions

after about 5 hours of work, I ended up with these two plots Temperature increase predictions CO2 emissions

I am not saying that those are exact replicas of economist plots, but those plots are close enough to economist plots. although we can create exact replicas of the oringinal plots with gridExtra package, which is also a good tool to create infographs in r, I didnt want to sacrifice my entire weekend for this project.

why should we have to stick with some theme designed by others, when you could unleash the creativity in you with your own themes. But why design a own theme? you may ask. well its very simple; a theme resonates with a paticular brand. if you observe publications of different media all their graphs looks similar. thats how I recognise the graph of economist from that of the other news sources such as guardian.

here is how you create a theme. we start with basic theme_bw and add required components to it

library()

newtheme <- theme_bw() + theme(line = element_line(colour = "black", size = 0.5, 
                      linetype = 1, lineend = "butt"), 
  rect = element_rect(fill = "white", colour = "black",
                      size = 0.5, linetype = 1),
  text = element_text(family = base_family, face = "plain",
                      colour = "black", size = base_size,
                      lineheight = 0.9,  hjust = 0.5,
                      vjust = 0.5, angle = 0, 
                      margin = margin(), debug = FALSE), 
  
  axis.line = element_blank(), 
  axis.text = element_text(size = rel(0.8), colour = "grey30"),
  axis.text.x = element_text(margin = margin(t = 0.8*half_line/2), 
                             vjust = 1), 
  axis.text.y = element_text(margin = margin(r = 0.8*half_line/2),
                             hjust = 1),
  axis.ticks = element_line(colour = "grey20"), 
  axis.ticks.length = unit(half_line/2, "pt"), 
  axis.title.x = element_text(margin = margin(t = 0.8 * half_line,
                                          b = 0.8 * half_line/2)),
  axis.title.y = element_text(angle = 90, 
                              margin = margin(r = 0.8 * half_line,
                                          l = 0.8 * half_line/2)),
  
  legend.background = element_rect(colour = NA), 
  legend.margin = unit(0.2, "cm"), 
  legend.key = element_rect(fill = "grey95", colour = "white"),
  legend.key.size = unit(1.2, "lines"), 
  legend.key.height = NULL,
  legend.key.width = NULL, 
  legend.text = element_text(size = rel(0.8)),
  legend.text.align = NULL,
  legend.title = element_text(hjust = 0), 
  legend.title.align = NULL, 
  legend.position = "right", 
  legend.direction = NULL,
  legend.justification = "center", 
  legend.box = NULL, 
  
  panel.background = element_rect(fill = "grey92", colour = NA),
  panel.border = element_blank(), 
  panel.grid.major = element_line(colour = "white"), 
  panel.grid.minor = element_line(colour = "white", size = 0.25), 
  panel.margin = unit(half_line, "pt"), panel.margin.x = NULL, 
  panel.margin.y = NULL, panel.ontop = FALSE, 
  
  strip.background = element_rect(fill = "grey85", colour = NA),
  strip.text = element_text(colour = "grey10", size = rel(0.8)),
  strip.text.x = element_text(margin = margin(t = half_line,
                                              b = half_line)), 
  strip.text.y = element_text(angle = -90, 
                              margin = margin(l = half_line, 
                                              r = half_line)),
  strip.switch.pad.grid = unit(0.1, "cm"),
  strip.switch.pad.wrap = unit(0.1, "cm"), 
  
  plot.background = element_rect(colour = "white"), 
  plot.title = element_text(size = rel(1.2), 
                            margin = margin(b = half_line * 1.2)),
  plot.margin = margin(half_line, half_line, half_line, half_line),
  complete = TRUE))

you can set the new theme as default theme by running this line or just add it to your plot

theme_set(newtheme)
#######################
ggplot(data = data, aes(x, y)) + 
        newtheme

finally a plot with a theme that i created

CO2 emissions

CO2 emissions

As a final word, lets discuss Tufte's principles for visualisation.
  • Every plot or a visulaisation is used to communicate a form of comparison. therefore, its absolute must to show the difference or the contrast.
  • If you are showing a casuality through a plot, make it conspicuous though annotations and labels.
  • A graph is an avenue to make the viewer understand large amounts of data in short time. That does'nt mean dumping lot of unnecessary and unrelated data on a single plot. Also, use graphs for multivariate data only.
  • A graph should tell the viewer a story, especially a credible one, so integrate every piece of evidence into the graph, such as text, numbers and images.
  • Make the lie factor of the graph close to one.
  • A visualisation should have high data ink, the ink that is used to represent the data, and low non data ink to keep the user attention on the data rather than on unwanted graphics. if you observe the Economist plots most of these Tuftes principles are followed in almost all the plots.