-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabeling the Components, Themes, Grids, Tick Marks, Legends, and Annotations
191 lines (147 loc) · 4.63 KB
/
Labeling the Components, Themes, Grids, Tick Marks, Legends, and Annotations
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
191
# Labeling the Components
## Title
library(gcookbook)
library(ggplot2)
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
geom_point()
hw_plot +
labs(title = "Age and Height of Schoolchildren")
hw_plot +
ggtitle("Age and Height of Schoolchildren", "11.5 to 17.5 years old")
hw_plot +
ggtitle("Age and Height\nof Schoolchildren")
## Axes
hw_plot +
labs(x = "Age in years", y = "Height in inches")
hw_plot +
xlab("Age in years") +
ylab("Height in inches")
hw_plot +
scale_x_continuous(name = "Age in years")
## Title of Legend
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
stat_summary(fun.y = mean, geom = "bar")
pg_plot
pg_plot + labs(x = "Age in Years",
y = "Height in Inches",
title = "Weight per Group",
fill = "Condition")
## Axis Ranges
pg_plot +
ylim(0, 10)
pg_plot +
scale_y_continuous(limits = c(0, 10))
pg_plot +
expand_limits(y = 0)
pg_plot +
scale_x_discrete(limits = c("ctrl", "trt1"))
## Date Axes
library(scales)
library(tidyverse)
econ_mod <- economics %>%
filter(date >= as.Date("1992-05-01") & date < as.Date("1993-06-01"))
datebreaks <- seq(as.Date("1992-06-01"), as.Date("1993-06-01"), by = "2 month")
econ_plot <- ggplot(econ_mod, aes(x = date, y = psavert)) +
geom_line()
econ_plot
econ_plot +
scale_x_date(breaks = datebreaks, labels = date_format("%Y %b")) +
theme(axis.text.x = element_text(angle = 30, hjust = 1))
# Themes
## Built-in Themes
hw_plot + theme_bw()
hw_plot + theme_minimal()
hw_plot + theme_classic()
hw_plot +
theme_grey(base_size = 16)
## Adjustments with Themes
hw_plot +
theme(axis.title.x = element_text(
size = 16, lineheight = .9,
face = "bold.italic", colour = "red"
))
hw_plot +
ggtitle("Age and Height\nof Schoolchildren") +
theme(plot.title = element_text(
size = rel(1.5), lineheight = .9,
face = "bold.italic", colour = "red"
))
## Main Plotting Area
hw_plot +
theme(
panel.grid.major = element_line(colour = "red"),
panel.grid.minor = element_line(colour = "red", linetype = "dashed", size = 0.2),
panel.background = element_rect(fill = "lightblue"),
panel.border = element_rect(colour = "blue", fill = NA, size = 2)
)
## Legend
hw_plot +
theme(
legend.background = element_rect(fill = "grey85", colour = "red", size = 1),
legend.title = element_text(colour = "blue", face = "bold", size = 14),
legend.text = element_text(colour = "red"),
legend.key = element_rect(colour = "blue", size = 0.25)
)
## Text Items in the Plot
hw_plot +
ggtitle("Plot title here") +
theme(
axis.title.x = element_text(colour = "red", size = 14),
axis.text.x = element_text(colour = "blue"),
axis.title.y = element_text(colour = "red", size = 14, angle = 90),
axis.text.y = element_text(colour = "blue"),
plot.title = element_text(colour = "red", size = 20, face = "bold")
)
## Options for Facets
hw_plot +
facet_grid(sex ~ .) +
theme(
strip.background = element_rect(fill = "pink"),
strip.text.y = element_text(size = 14, angle = -90, face = "bold")
)
# Grids and Tick Marks
hw_plot +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
hw_plot +
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()
)
hw_plot +
scale_x_continuous(breaks = c(12, 14, 15, 16, 18))
hw_plot +
scale_x_continuous(breaks = seq(12, 18, by = .5))
pg_plot +
scale_x_discrete(labels = c("Control", "T1", "T2"),
position = "top")
# Legends
## Removing the Legend
pg_plot +
guides(fill = FALSE)
pg_plot +
scale_fill_discrete(guide = FALSE)
## Legend Position
pg_plot +
theme(legend.position = "top")
## Order in Legend
pg_plot +
scale_fill_discrete(limits = c("trt1", "trt2", "ctrl"))
# Annotations
p <- ggplot(data.frame(x = c(-3, 3)), aes(x = x)) +
stat_function(fun = dnorm)
p +
annotate("text", x = 2, y = 0.3, parse = TRUE,
label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
p <- ggplot(filter(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
p +
annotate("rect", xmin = 1950, xmax = 1980, ymin = -1, ymax = 1,
alpha = .1, fill = "blue")
pg_mod <- PlantGrowth %>%
mutate(hl = recode(group, "ctrl" = "no", "trt1" = "no", "trt2" = "yes"))
ggplot(pg_mod, aes(x = group, y = weight, fill = hl)) +
geom_boxplot() +
scale_fill_manual(values = c("grey85", "#FFDDCC"), guide = FALSE)