File: ggplot2-specs.R

package info (click to toggle)
r-cran-ggplot2 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,084 kB
  • sloc: sh: 15; makefile: 5
file content (190 lines) | stat: -rw-r--r-- 7,686 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
## -----------------------------------------------------------------------------
#| include: false
library(ggplot2)


## -----------------------------------------------------------------------------
munsell::mnsl("5PB 5/10")


## -----------------------------------------------------------------------------
#| fig.alt: "A series of 6 horizontal lines with different line types.
#|  From top-to-bottom they are titled 'solid', 'dashed', 'dotted',
#|  'dotdash', 'longdash', 'twodash'."
lty <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
linetypes <- data.frame(
  y = seq_along(lty),
  lty = lty
)
ggplot(linetypes, aes(0, y)) +
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) +
  scale_linetype_identity() +
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
  scale_x_continuous(NULL, breaks = NULL) +
  scale_y_reverse(NULL, breaks = NULL)


## -----------------------------------------------------------------------------
#| fig.alt: "A series of 9 horizontal lines with different line types.
#|  Each line is titled by two hexadecimal digits that determined the
#|  lengths of dashes and gaps."
lty <- c("11", "18", "1f", "81", "88", "8f", "f1", "f8", "ff")
linetypes <- data.frame(
  y = seq_along(lty),
  lty = lty
)
ggplot(linetypes, aes(0, y)) +
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) +
  scale_linetype_identity() +
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
  scale_x_continuous(NULL, breaks = NULL) +
  scale_y_reverse(NULL, breaks = NULL)


## -----------------------------------------------------------------------------
#| out-width: 30%
#| fig-show: hold
#| fig.alt:
#| - "A plot showing a line with an angle. A thinner red line is placed over
#|  a thicker black line. The black line ends where the red line ends."
#| - "A plot showing a line with an angle. A thinner red line is placed over
#|  a thicker black line. The black line ends past where the red line ends,
#|  and ends in a semicircle."
#| - "A plot showing a line with an angle. A thinner red line is placed over
#|  a thicker black line. The black line ends past where the red line ends,
#|  and ends in a square shape."
df <- data.frame(x = 1:3, y = c(4, 1, 9))
base <- ggplot(df, aes(x, y)) + xlim(0.5, 3.5) + ylim(0, 10)
base +
  geom_path(linewidth = 10) +
  geom_path(linewidth = 1, colour = "red")

base +
  geom_path(linewidth = 10, lineend = "round") +
  geom_path(linewidth = 1, colour = "red")

base +
  geom_path(linewidth = 10, lineend = "square") +
  geom_path(linewidth = 1, colour = "red")


## -----------------------------------------------------------------------------
#| out-width: 30%
#| fig-show: hold
#| fig.alt:
#| - "A plot showing a thin red line on top of a thick black line shaped like
#|  the letter 'V'. The corner in the black V-shape is rounded."
#| - "A plot showing a thin red line on top of a thick black line shaped like
#|  the letter 'V'. The corner in the black V-shape is sharp."
#| - "A plot showing a thin red line on top of a thick black line shaped like
#|  the letter 'V'. A piece of the corner is cut off so that the two
#|  straight parts are connected by a horizontal part."
df <- data.frame(x = 1:3, y = c(9, 1, 9))
base <- ggplot(df, aes(x, y)) + ylim(0, 10)
base +
  geom_path(linewidth = 10) +
  geom_path(linewidth = 1, colour = "red")

base +
  geom_path(linewidth = 10, linejoin = "mitre") +
  geom_path(linewidth = 1, colour = "red")

base +
  geom_path(linewidth = 10, linejoin = "bevel") +
  geom_path(linewidth = 1, colour = "red")


## -----------------------------------------------------------------------------
#| fig.alt: "A 5-by-5 grid of point symbols annotated by the numbers
#|  that can be used to represent the symbols. From left to right, the
#|  first 15 symbols are lines or open shapes, the next 5 symbols are solid
#|  shapes and the last 5 symbols are filled shaped."
shapes <- data.frame(
  shape = c(0:19, 22, 21, 24, 23, 20),
  x = 0:24 %/% 5,
  y = -(0:24 %% 5)
)
ggplot(shapes, aes(x, y)) +
  geom_point(aes(shape = shape), size = 5, fill = "red") +
  geom_text(aes(label = shape), hjust = 0, nudge_x = 0.15) +
  scale_shape_identity() +
  expand_limits(x = 4.1) +
  theme_void()


## -----------------------------------------------------------------------------
#| out-width: 90%
#| fig-asp: 0.4
#| fig-width: 8
#| fig.alt: "An irregular 6-by-7 grid of point symbols annotated by the
#|  names that can be used to represent the symbols. Broadly, from top to
#|  bottom, the symbols are circles, squares, diamonds, triangles and
#|  others. Broadly from left to right, the symbols are solid shapes,
#|  open shapes, filled shapes and others."
shape_names <- c(
  "circle", paste("circle", c("open", "filled", "cross", "plus", "small")), "bullet",
  "square", paste("square", c("open", "filled", "cross", "plus", "triangle")),
  "diamond", paste("diamond", c("open", "filled", "plus")),
  "triangle", paste("triangle", c("open", "filled", "square")),
  paste("triangle down", c("open", "filled")),
  "plus", "cross", "asterisk"
)

shapes <- data.frame(
  shape_names = shape_names,
  x = c(1:7, 1:6, 1:3, 5, 1:3, 6, 2:3, 1:3),
  y = -rep(1:6, c(7, 6, 4, 4, 2, 3))
)

ggplot(shapes, aes(x, y)) +
  geom_point(aes(shape = shape_names), fill = "red", size = 5) +
  geom_text(aes(label = shape_names), nudge_y = -0.3, size = 3.5) +
  scale_shape_identity() +
  theme_void()


## -----------------------------------------------------------------------------
#| fig.alt: "A plot showing a 4-by-4 grid of red points, the top 12 points with
#|  black outlines. The size of the points increases horizontally. The stroke of
#|  the outlines of the points increases vertically. A white diagonal line with
#|  a negative slope marks that the 'stroke' versus 'size' trade-off has
#|  similar total sizes."
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
  geom_abline(slope = -1, intercept = 6, colour = "white", linewidth = 6) +
  geom_point(shape = 21, fill = "red") +
  scale_size_identity()


## -----------------------------------------------------------------------------
#| fig.alt: "A plot showing three text labels arranged vertically. The top
#|  label is 'sans' and is displayed in a sans-serif font. The middle label is
#|  'serif' and is displayed in a serif font. The bottom label is 'mono' and
#|  is displayed in a monospaced font."
df <- data.frame(x = 1, y = 3:1, family = c("sans", "serif", "mono"))
ggplot(df, aes(x, y)) +
  geom_text(aes(label = family, family = family))


## -----------------------------------------------------------------------------
#| fig.alt: "A plot showing four text labels arranged vertically. The top
#|  label is 'bold.italic' and is displayed in bold and italic. The next three
#|  labels are 'italic', 'bold' and 'plain' and are displayed in their
#|  respective styles."
df <- data.frame(x = 1:4, fontface = c("plain", "bold", "italic", "bold.italic"))
ggplot(df, aes(1, x)) +
  geom_text(aes(label = fontface, fontface = fontface))


## -----------------------------------------------------------------------------
#| fig.alt: "A 3-by-3 grid of text on top of points, with horizontal text
#|  justification increasing from 0 to 1 on the x-axis and vertical
#|  justification increasing from 0 to 1 on the y-axis. The points make it
#|  easier to see the relative placement of text."
just <- expand.grid(hjust = c(0, 0.5, 1), vjust = c(0, 0.5, 1))
just$label <- paste0(just$hjust, ", ", just$vjust)

ggplot(just, aes(hjust, vjust)) +
  geom_point(colour = "grey70", size = 5) +
  geom_text(aes(label = label, hjust = hjust, vjust = vjust))