-
Notifications
You must be signed in to change notification settings - Fork 1
/
R-ggText.R
172 lines (129 loc) · 3.85 KB
/
R-ggText.R
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
# TIDY TUES
library(tidyverse)
library(scales)
library(ggplot2)
library(lubridate)
library(stringi)
library(stringr)
library(ggtext)
# ----- ggtext
base <- ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = ~ .x*.x)
base
# ------ markdown text
base +
labs(
x = "independent variable *x*",
y = "dependent variable *y* = *x*<sup>2</sup>"
) +
theme(
axis.title.x = element_markdown(),
axis.title.y = element_markdown()
)
# ----- makrdown w/ color + size
base +
labs(
x = "independent variable *x*",
y = "dependent variable *y* = *x*<sup>2</sup>"
) +
theme(
axis.title.x = element_markdown(color = "blue"),
axis.title.y = element_markdown(size = rel(0.8))
)
base +
labs(
x = "independent variable *x*",
y = "dependent variable *y* = *x*<sup>2</sup>"
) +
theme(
axis.title = element_text(color = "blue", size = rel(0.8)),
axis.title.x = element_markdown(),
axis.title.y = element_markdown()
)
base +
labs(
x = "independent variable *x*",
y = "dependent variable *y* = *x*<sup>2</sup>"
) +
scale_y_continuous(position = "right") +
theme(
axis.title = element_text(color = "purple", size = rel(0.8)),
axis.title.x = element_markdown(),
axis.title.y.right = element_markdown()
)
library(dplyr)
mtcars %>%
mutate(
transmission = ifelse(am == 1, "automatic", "manual")
) %>%
ggplot(aes(hp, mpg, color = transmission)) +
geom_point(size = 2) +
scale_color_manual(
values = c(automatic = "#0072B2", manual = "#D55E00"),
guide = "none"
) +
labs(
x = "Horse power",
y = "Miles per gallon (MPG)",
title = "<span style = 'font-size:14pt; font-family:Helvetica;'>Transmission type impacts fuel efficiency</span><br>
MPG is higher for <span style = 'color:#0072B2;'>automatic</span>
than for <span style = 'color:#D55E00;'>manual</span> transmissions"
) +
theme_bw() +
theme(
text = element_text(family = "Times"),
plot.title.position = "plot",
plot.title = element_markdown(size = 11, lineheight = 1.2)
)
base <- mtcars %>%
mutate(
transmission = ifelse(am == 1, "automatic", "manual")
) %>%
ggplot(aes(hp, mpg, color = transmission)) +
geom_point(size = 2) +
scale_color_manual(
values = c(automatic = "#0072B2", manual = "#D55E00"),
guide = "none"
) +
labs(
x = "Horse power",
y = "Miles per gallon (MPG)",
title = "Transmission type impacts fuel efficiency<br>
<span style = 'font-size:10pt;'>Miles per gallon (MPG) is on average higher for cars
with <span style = 'color:#0072B2;'>automatic transmission</span> than for cars with
<span style = 'color:#D55E00;'> manual transmission.</span> However, MPG generally
declines with increasing horse power.</span>"
) +
theme_bw() + theme(plot.title.position = "plot")
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1, padding = margin(0, 0, 5, 0)
)
)
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1,
linetype = 1, # turn on border
box.color = "#748696", # border color
fill = "#F0F7FF", # background fill color
r = grid::unit(3, "pt"), # radius for rounded corners
padding = margin(5, 5, 5, 5), # padding around text inside the box
margin = margin(0, 0, 10, 0) # margin outside the box
)
)
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1,
width = grid::unit(4, "in"), # fixed width
hjust = 1, # alignment of box relative to plot
linetype = 1, # turn on border
box.color = "#748696", # border color
fill = "#F0F7FF", # background fill color
r = grid::unit(3, "pt"), # radius for rounded corners
padding = margin(5, 5, 5, 5), # padding around text inside the box
margin = margin(0, 0, 10, 0) # margin outside the box
)
)