Using ggplot Themes For In-Print Visualization

6 February 2018
By Quinn Schiller

While most of the recent conversation around data visualization has focused on optimizing graphs for online viewing (and rightfully so) , some graphs are still made for production in books. In a current project, I have to design visualizations to the specifications of a publisher.

These restrictions include

  • Size constraintsm
    • All graphs must be a specific size (and it’s pretty small)
    • No whitespace borders allowed
    • Minimize size of legends
  • Print constraints
    • No excessively thin lines
    • Only black and white (no color or grayscale)
    • No incredibly small fonts
  • Graph Constraints
    • All graphs must have a comprehensive theme
      • Same font size
      • Same axis title layout
      • Same line thickness for everything ect.

One (incorrect) way to take on this problem is to try to tackle each graph one at a time to make it fit these problems. This may work to fit size and print constraints, but as the project grows and changes, it will be hard to maintain a cohesive aesthetic between visualizations as graphs are changed and more client requests come in.

The correct way to overcome these issues is through the power of ggplot themes. I’ve written about ggplot in other blog posts but it is a library that allows graphs to be made and modified in a more organic way in R and provides additional customization options. By making a singular ggplot theme that all graphs can use, this insures that all ggplot graphs will have a comprehensive look to them. Additionally, whenever the theme is changed, all graphs will automatically update themselves every time the project is ran.  The theme itself can also be made to fit publisher size and print standards by putting size and color values into the theme itself, further saving time.

This process also doesn’t sacrifice flexibility later on in the project. All settings from the custom theme can be overridden when constructing the graph to locally superseed options if the graph requires it.

[wmd-accordion tab_background=”#066196″ tab_color=”#fff” content_background=”#2196d1″ content_color=”#fff” border_radius=”4″ ls-id=”5a7a2ca1b7fda”][wmd-accordion-tab title=”Example Theme Code”]%3Cpre%3ElineW %26lt%3B- 0.3%3Cbr %2F%3E%3Cbr %2F%3EcustomTheme %26lt%3B- theme_bw%28%29 %2B theme%28%3Cbr %2F%3E strip.background %3D element_blank%28%29%2C%3Cbr %2F%3E strip.placement %3D %22outside%22%2C%3Cbr %2F%3E text %3D element_text%28%3Cbr %2F%3E size %3D 10%2C%3Cbr %2F%3E family %3D %22Arial%22%2C%3Cbr %2F%3E face %3D %22bold%22%2C%3Cbr %2F%3E color %3D %22black%22%29%2C%3Cbr %2F%3E legend.position %3D %22bottom%22%2C%3Cbr %2F%3E strip.text %3D element_text%28hjust %3D 0.5%2C face %3D %22bold%22%29%2C%3Cbr %2F%3E legend.title %3D element_blank%28%29%2C%3Cbr %2F%3E plot.caption %3D element_text%28hjust %3D 0.5%2C %3Cbr %2F%3E face %3D%22italic%22%2C %3Cbr %2F%3E size %3D 8%29%2C%3Cbr %2F%3E plot.margin %3D margin%28%3Cbr %2F%3E t %3D 0%2C%3Cbr %2F%3E r %3D 0%2C%3Cbr %2F%3E b %3D 0%2C%3Cbr %2F%3E l %3D 0%2C%3Cbr %2F%3E unit %3D %22cm%22%29%2C%3Cbr %2F%3E legend.margin %3D margin%28%3Cbr %2F%3E t %3D 0%2C%3Cbr %2F%3E r %3D 0%2C%3Cbr %2F%3E b %3D 0%2C%3Cbr %2F%3E l %3D 0%2C%3Cbr %2F%3E unit %3D %22cm%22%29%2C%3Cbr %2F%3E legend.key.height %3D unit%280%2C %22cm%22%29%2C%3Cbr %2F%3E strip.switch.pad.wrap %3D unit%280%2C %22cm%22%29%2C%3Cbr %2F%3E panel.grid.major %3D element_line%28colour %3D %22black%22%2C size %3D 0.05%29%2C%3Cbr %2F%3E panel.grid.minor %3D element_line%28colour %3D %22white%22%29%3Cbr %2F%3E%29%3Cbr %2F%3E%3Cbr %2F%3EnoY %26lt%3B- theme%28axis.title.y %3D element_blank%28%29%29 %23for removing space taken up by removed y axis%3Cbr %2F%3E%3Cbr %2F%3E%3C%2Fpre%3E%0D%0A%3Cp%3EAdditional options only used by some graphs can be made into separate themes to make the code more readable %28in noY%29.%C2%A0%3C%2Fp%3E[/wmd-accordion-tab][wmd-accordion-tab title=”Example Graph Code That Uses the Theme “]%3Cpre%3Eg %26lt%3B- ggplot%28df%29 %2B%3Cbr %2F%3E geom_line%28mapping %3D aes%28x %3D day%2C y %3D value%2C linetype %3D Party%29%2C %3Cstrong%3Esize %3D lineW%3C%2Fstrong%3E%29%2B %23 assign graph aesthetics%3Cbr %2F%3E facet_wrap%28~ variable%2C ncol %3D 1%2Cscales %3D %5C%27free_y%5C%27%2Cstrip.position %3D %22left%22%29 %2B %23facet %3Cbr %2F%3E scale_x_continuous%28name %3D %22Days until Election%22%2C trans %3D %22reverse%22%29 %2B %23name and reverse x axis%3Cbr %2F%3E scale_y_continuous%28labels %3D comma%29 %2B %23don%5C%27t use scientific notation%3Cbr %2F%3E labs%28caption %3D %22%2AOnly ads mentioning at least one issue. Dotted vertical line on day 50 marks Lehman Brother collapse.%22%29 %2B %23caption%3Cbr %2F%3E scale_linetype_manual%28values%3Dc%28%22solid%22%2C %22longdash%22%29%29 %2B %23 line aesthetic change%3Cbr %2F%3E %3Cstrong%3EcustomTheme %2B noY %23 apply custom theme%3C%2Fstrong%3E%3C%2Fpre%3E[/wmd-accordion-tab][wmd-accordion-tab title=”Example Graph Code That Uses the Theme as a Base But Modifies it”]%3Cpre%3Eg %26lt%3B- ggplot%28df%29 %2B%3Cbr %2F%3E geom_line%28mapping %3D aes%28x %3D day%2C y %3D value%2C linetype %3D Party%29%2C size %3D lineW%29%2B %23 assign graph aesthetics%3Cbr %2F%3E facet_wrap%28~ variable%2C ncol %3D 1%2Cscales %3D %5C%27free_y%5C%27%2Cstrip.position %3D %22left%22%29 %2B %23facet %3Cbr %2F%3E scale_x_continuous%28name %3D %22Days until Election%22%2C trans %3D %22reverse%22%29 %2B %23name and reverse x axis%3Cbr %2F%3E scale_y_continuous%28labels %3D comma%29 %2B %23don%5C%27t use scientific notation%3Cbr %2F%3E labs%28caption %3D %22%2AOnly ads mentioning at least one issue. Dotted vertical line on day 50 marks Lehman Brother collapse.%22%29 %2B %23caption%3Cbr %2F%3E scale_linetype_manual%28values%3Dc%28%22solid%22%2C %22longdash%22%29%29 %2B %23 line aesthetic change%3Cbr %2F%3E customTheme %2B noY%2B%3Cstrong%3E theme%28legend.position %3D %22right%22%2C legend.key.height %3D unit%280.6%2C %22cm%22%29%29%3C%2Fstrong%3E%3C%2Fpre%3E%0D%0A%3Cp%3EAdding additional theme options%2Foverriding theme options from the custom theme is very simple.%3C%2Fp%3E[/wmd-accordion-tab][/wmd-accordion]

In summary, using ggplot themes not only is a good best practice but also can save time and effort on long projects.