Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Line Graphs > Changing the Appearance of Points

Changing the Appearance of Points

Problem

You want to change the appearance of the points in a line graph.

Solution

In geom_point(), set the size, shape, colour, and/or fill outside of aes() (the result is shown in Figure 4-14):

ggplot(BOD, aes(x=Time, y=demand)) +
    geom_line() +
    geom_point(size=4, shape=22, colour="darkred", fill="pink")
Points with custom size, shape, color, and fill
Figure 4-14. Points with custom size, shape, color, and fill

Discussion

The default shape for points is a solid circle, the default size is 2, and the default colour is "black". The fill color is relevant only for some point shapes (numbered 21–25), which have separate outline and fill colors (see Using Different Point Shapes for a chart of shapes). The fill color is typically NA, or empty; you can fill it with white to get hollow-looking circles, as shown in Figure 4-15:

ggplot(BOD, aes(x=Time, y=demand)) +
    geom_line() +
    geom_point(size=4, shape=21, fill="white")
Points with a white fill
Figure 4-15. Points with a white fill

If the points and lines have different colors, you should specify the points after the lines, so that they are drawn on top. Otherwise, the lines will be drawn on top of the points.

For multiple lines, we saw in Making a Line Graph with Multiple Lines how to draw differently colored points for each group by mapping variables to aesthetic properties of points, inside of aes(). The default colors are not very appealing, so you may want to use a different palette, using scale_colour_brewer() or scale_colour_manual(). To set a single constant shape or size for all the points, as in Figure 4-16, specify shape or size outside of aes():

# Load plyr so we can use ddply() to create the example data set
library(plyr)
# Summarize the ToothGrowth data
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

# Save the position_dodge specification because we'll use it multiple times
pd <- position_dodge(0.2)

ggplot(tg, aes(x=dose, y=length, fill=supp)) +
    geom_line(position=pd) +
    geom_point(shape=21, size=3, position=pd) +
    scale_fill_manual(values=c("black","white"))
With a manually specified fill of black and white, and a
            slight dodge
Figure 4-16. Line graph with manually specified fills of black and white, and a slight dodge

See Also

See Using Different Point Shapes for more on using different shapes, and Chapter 12 for more about colors.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint