Even the with many defaults, the explicit grammar syntax is rather verbose, and usually spans multiple lines. This makes it more difficult to rapidly experiment with many different plots. For this reason the verbose grammar is supplemented with the qplot
(short for quick plot) function which makes strong assumptions to reduce the amount of typing needed. It also mimics the syntax of the plot
function so it is easy to use for people already familiar with base graphics.
qplot
assumes that multiple layers will use the same data source, and defaults to creating a scatterplot. This allows us to replace the following explicit, but verbose code:
ggplot() + layer( data = diamonds, mapping = aes(x = carat, y = price), geom = "point", stat = "identity" ) + scale_y_continuous() + scale_x_continuous() + coord_cartesian()
with:
qplot(carat, price, data = dsmall)
And this code:
ggplot() + layer( data = diamonds, mapping = aes(x = carat, y = price), geom = "point", stat = "identity" ) + layer( data = diamonds, mapping = aes(x = carat, y = price), geom = "smooth", stat = "smooth", method = lm ) + scale_y_log10() + scale_x_log10()
with this
qplot(carat, price, data = dsmall, geom = c("smooth", "point"), method = "lm", log = "xy")
Note the use of the log
argument which specifies which axes should be log-transformed. This mimics the plot
function in base R.
You can start to see the limitations of this approach as it is ambiguous which geom the method argument applies to. The qplot
function will break down for complicated examples, but is still useful for quick, disposable plots.
The qplot function creates a plot object in the same way that gplot
does, so you can modify it in the same way:
qplot(carat, price, data = dsmall) + scale_x_log10() qplot(carat, price, data = dsmall) + geom_smooth()
This makes it possible to start with a simple quick plot and progressively build it up to produce a richer plot.