
How it works...
The ggplot2 library is an open source implementation of the foundational grammar of graphics by Wilkinson, Anand, and Grossman for R. The Grammar of Graphics attempts to decompose statistical data visualizations into component parts to better understand how such graphics are created. With ggplot2, Hadley Wickham takes these ideas and implements a layered approach, allowing the user to assemble complex visualizations from individual pieces very quickly. Take, for example, the first graph for this recipe, which shows the average fuel efficiency of all models of cars in a particular year over time:
ggplot(mpgByYr, aes(year, avgMPG)) + geom_point() + geom_smooth() +
xlab("Year") + ylab("Average MPG") + ggtitle("All cars")
To construct this plot, we first tell ggplot the data frame that will serve as the data for the plot (mpgByYr), and then the aesthetic mappings that will tell ggplot2 which variables will be mapped into visual characteristics of the plot. In this case, aes(year, avgMPG) implicitly specifies that the year will be mapped to the x axis and avgMPG will be mapped to the y axis. geom_point() tells the library to plot the specified data as points and a second geom, geom_smooth(), adds a shaded region showing the smoothed mean (with a confidence interval set to 0.95, by default) for the same data. Finally, the xlab(), ylab(), and ggtitle() functions are used to add labels to the plot. Thus, we can generate a complex, publication quality graph in a single line of code; ggplot2 is capable of doing far more complex plots.
Also, it is important to note that ggplot2, and the grammar of graphics in general, does not tell you how best to visualize your data, but gives you the tools to do so rapidly. If you want more advice on this topic, we strongly recommend looking into the works of Edward Tufte, who has numerous books on the matter, including the classic The Visual Display of Quantitative Information, Graphics Press USA. Furthermore, ggplot2 does not allow for dynamic data visualizations.