These geoms are the fundamental building blocks of ggplot2. They are useful in their own right, but are also used to construct more complex geoms. Most of these geoms are associated with a named plot: when that geom is used by itself in a plot, that plot has a special name.
Each of these geoms is two dimensional and requires both
y aesthetics. All of them understand
size aesthetics, and the filled geoms (bar, tile and polygon) also understand
geom_area()draws an area plot, which is a line plot filled to the y-axis (filled lines). Multiple groups will be stacked on top of each other.
geom_bar(stat = "identity")makes a bar chart. We need
stat = "identity"because the default stat automatically counts values (so is essentially a 1d geom, see Section 5.4. The identity stat leaves the data unchanged. Multiple bars in the same location will be stacked on top of one another.
geom_line()makes a line plot. The
groupaesthetic determines which observations are connected; see Chapter 4 for more detail.
geom_line()connects points from left to right;
geom_path()is similar but connects points in the order they appear in the data. Both
geom_path()also understand the aesthetic
linetype, which maps a categorical variable to solid, dotted and dashed lines.
geom_polygon()draws polygons, which are filled paths. Each vertex of the polygon requires a separate row in the data. It is often useful to merge a data frame of polygon coordinates with the data just prior to plotting. Section 6 illustrates this concept in more detail for map data.
geom_rect()is parameterised by the four corners of the rectangle,
geom_tile()is exactly the same, but parameterised by the center of the rect and its size,
geom_raster()is a fast special case of
geom_tile()used when all the tiles are the same size. .
Each geom is shown in the code below. Observe the different axis ranges for the bar, area and tile plots: these geoms take up space outside the range of the data, and so push the axes out.
df <- data.frame( x = c(3, 1, 5), y = c(2, 4, 6), label = c("a","b","c") ) p <- ggplot(df, aes(x, y, label = label)) + labs(x = NULL, y = NULL) + # Hide axis label theme(plot.title = element_text(size = 12)) # Shrink plot title p + geom_point() + ggtitle("point") p + geom_text() + ggtitle("text") p + geom_bar(stat = "identity") + ggtitle("bar") p + geom_tile() + ggtitle("raster")
What geoms would you use to draw each of the following named plots?
- Line chart
- Bar chart
- Pie chart