We have learned the powerful approach to generating visualization with ggplot. However, there are instances in which all we want is to make a quick plot of, for example, a histogram of the values in a vector, a scatterplot of the values in two vectors, or a boxplot using categorical and numeric vectors. We demonstrated how to generate these plots with hist, plot, and boxplot. However, if we want to keep consistent with the ggplot style, we can use the function qplot.

If we have values in two vectors, say:

data(murders)
x <- log10(murders$population)
y <- murders$total

and we want to make a scatterplot with ggplot, we would have to type something like:

data.frame(x = x, y = y) %>%
  ggplot(aes(x, y)) +
  geom_point()

This seems like too much code for such a simple plot. The qplot function sacrifices the flexibility provided by the ggplot approach, but allows us to generate a plot quickly.

qplot(x, y)

We will learn more about qplot in Section 8.16.