5.12 Add themes to graphs - Video Tutorials & Practice Problems
Video duration:
2m
Play a video:
<v Voiceover>Ggplot2</v> is highly extensible, with lots of customizations possible. One of these is a theming system that lets you put together commonly used themes. There's a package called ggthemes from Jeffrey Arnold that really makes this easy to use, so let's load up his package and see what it can do. Require ggthemes. Now, let's build up our base graph which is gonna be similar as before, ggplot diamonds, aes, x equals carat, y equals price, and color equals color. And we will add in the geom underscore point, so that way it makes a scatter plot. Looking at that, it is our familiar graph. Now, it's possible with one extra line of code to make it look like it was put out by the Wall Street Journal. This has their signature off white color paper and their colors. Similarly, you can make it look like it is from The Economist. Even with that you can add in a special color pallet that The Economist is known to use. In this situation it even moved the legend on top of the graph which is the normal placement for The Economist. There is also a theme that would make Edward Tufte very happy. Remember he is in favor of a lot of minimalism, so there is not a lot of stuff here. Very, very bare bones graph. And lastly, just for giggles, there is a theme that can make it look like your graph came from Excel. Right here everything in ggplot must be separated by plus signs. We have an Excel looking graph. Not that it's necessarily anything wrong with this, it's just if you have this beautiful graphing capability I would go with The Economist or Tufte or Wall Street Journal over Excel. With ggplot2 and this package you can use some beautiful pre-built themes or you can create your own theme that has your own flavor to it.