2.5 Plot geoms 13
attribute, there is a function, called a scale, which maps data values to valid
values for that aesthetic. It is this scale that controls the appearance of the
points and associated legend. For example, in the above plots, the colour
scale maps J to purple and F to green. (Note that while I use British spelling
throughout this book, the software also accepts American spellings.)
You can also manually set the aesthetics using I(), e.g., colour = I("red")
or size = I(2). This is not the same as mapping and is explained in more
detail in Section 4.5.2. For large datasets, like the diamonds data, semi-
transparent points are often useful to alleviate some of the overplotting. To
make a semi-transparent colour you can use the alpha aesthetic, which takes
a value between 0 (completely transparent) and 1 (complete opaque). It’s
often useful to specify the transparency as a fraction, e.g., 1/10 or 1/20,as
the denominator specifies the number of points that must overplot to get a
completely opaque colour.
qplot(carat, price, data = diamonds, alpha = I(1/10))
qplot(carat, price, data = diamonds, alpha = I(1/100))
qplot(carat, price, data = diamonds, alpha = I(1/200))
Fig. 2.3: Reducing the alpha value from 1/10 (left) to 1/100 (middle) to 1/200 (right)
makes it possible to see where the bulk of the points lie.
Different types of aesthetic attributes work better with different types of
variables. For example, colour and shape work well with categorical variables,
while size works better with continuous variables. The amount of data also
makes a difference: if there is a lot of data, like in the plots above, it can
be hard to distinguish the different groups. An alternative solution is to use
faceting, which will be introduced in Section
2.6.
2.5 Plot geoms
qplot is not limited to scatterplots, but can produce almost any kind of plot
by varying the geom. Geom, short for geometric object, describes the type