forked from hadley/ggplot2-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
individual-geoms.qmd
99 lines (80 loc) · 4.14 KB
/
individual-geoms.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Individual geoms {#sec-individual-geoms}
```{r}
#| echo: false
#| message: false
#| results: asis
source("common.R")
status("drafting")
```
## Basic plot types {#sec-basics}
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 `x` and `y` aesthetics.
All of them understand `colour` (or `color`) and `size` aesthetics, and the filled geoms (bar, tile and polygon) also understand `fill`.
- `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.
\index{Area plot} \indexf{geom\_area}
- `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 @sec-distributions).
The identity stat leaves the data unchanged.
Multiple bars in the same location will be stacked on top of one another.\index{Barchart} \indexf{geom\_bar}
- `geom_line()` makes a **line plot**.
The `group` aesthetic determines which observations are connected; see @sec-collective-geoms 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_line()` and `geom_path()` also understand the aesthetic `linetype`, which maps a categorical variable to solid, dotted and dashed lines.
\index{Line plot} \indexf{geom\_line} \indexf{geom\_path}
- `geom_point()` produces a **scatterplot**.
`geom_point()` also understands the `shape` aesthetic.
\indexf{geom\_point}
- `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.
@sec-maps illustrates this concept in more detail for map data.
\indexf{geom\_polygon}
- `geom_rect()`, `geom_tile()` and `geom_raster()` draw rectangles.
`geom_rect()` is parameterised by the four corners of the rectangle, `xmin`, `ymin`, `xmax` and `ymax`.
`geom_tile()` is exactly the same, but parameterised by the center of the rect and its size, `x`, `y`, `width` and `height`.
`geom_raster()` is a fast special case of `geom_tile()` used when all the tiles are the same size.
\index{Image plot} \index{Level plot} \indexf{geom\_tile}.
\indexf{geom\_rect} \indexf{geom\_raster}
- `geom_text()` adds text to a plot.
It requires a `label` aesthetic that provides the text to display, and has a number of parameters (`angle`, `family`, `fontface`, `hjust` and `vjust`) that control the appearance of the text.
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.
```{r}
#| label: geom-basic
#| layout-ncol: 4
#| fig-width: 2.5
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")
```
```{r}
#| layout-ncol: 4
#| fig-width: 2.5
p + geom_line() + ggtitle("line")
p + geom_area() + ggtitle("area")
p + geom_path() + ggtitle("path")
p + geom_polygon() + ggtitle("polygon")
```
### Exercises
1. What geoms would you use to draw each of the following named plots?
1. Scatterplot
2. Line chart
3. Histogram
4. Bar chart
5. Pie chart
2. What's the difference between `geom_path()` and `geom_polygon()`?
What's the difference between `geom_path()` and `geom_line()`?
3. What low-level geoms are used to draw `geom_smooth()`?
What about `geom_boxplot()` and `geom_violin()`?