ABSTRACT

Graphics is an important part of reports, and a lot of efforts have been made in knitr to make sure graphics output is natural and flexible. For example, knitr tries to mimic the behavior of the R console, and grid graphics (Murrell, 2011) may not need to be explicitly printed as long as the same code can produce plots in the R console (in some cases, however, they have to be printed, e.g., in a loop, because we have to do so in an R console); below is a chunk of code that will produce a plot in both the R console and knitr (see Figure 7.1):

library(ggplot2) p <- qplot(carat, price, data = diamonds) + geom_hex() p # no need to print(p)

FIGURE 7.1: A plot created in ggplot2 that does not need to be printed explicitly (by comparison, we have to print(p) in Sweave, which is very confusing; see Section 15.1).