-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathR_code_participant.R
202 lines (155 loc) · 6.15 KB
/
R_code_participant.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Random number seed
set.seed(5)
# Scatter plot
plot(rnorm(20), rnorm(20))
import::from("ggplot2", "aes", "vars", "mpg")
# Examine the mpg dataset
dplyr::glimpse(mpg)
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point()
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::geom_point(aes(x = displ, y = cty), colour = "blue")
## # chart template
## ggplot(data = <DATA>, aes(<MAPPINGS>) ) +
## <GEOM_FUNCTION> +
## <COORDINATE_FUNCTION> +
## <FACET_FUNCTION> +
## <SCALE_FUNCTION> +
## <THEME FUNCTION>
# Aesthetics
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = class)) +
ggplot2::geom_point()
# ggplot2 looks at the data type to set a default colour scale
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = year)) +
ggplot2::geom_point()
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, shape = class, colour = class)) +
ggplot2::geom_point()
# Use scale_ functions to set properties of the aesthetics
dplyr::filter(mpg, manufacturer %in% c('audi', 'volkswagen')) |>
ggplot2::ggplot(aes(x = displ, y = hwy, colour = manufacturer)) +
ggplot2::geom_point(size = 3) +
ggplot2::scale_colour_manual(values = c("audi" = "#12436D", "volkswagen" = "#28A197")) +
ggplot2::scale_x_continuous(limits = c(1, 5))
# Set an aesthetic to a fixed value
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point(colour = "orange")
# What's wrong with this code? Why aren't the points blue?
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = "blue")) +
ggplot2::geom_point()
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::facet_wrap(vars(class))
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::facet_grid(rows = vars(drv), cols = vars(class))
# Flat data
head(mpg)
# Bar chart counting rows in the data
ggplot2::ggplot(mpg, aes(x = class)) +
ggplot2::geom_bar()
# Sometimes you might start with tabulated, rather than flat data
mpg_summary <- mpg |>
dplyr::group_by(class) |>
dplyr::summarise(count = dplyr::n())
head(mpg_summary)
# Bar charts from tabulated data
mpg_summary |>
ggplot2::ggplot(aes(x = class, y = count)) +
ggplot2::geom_col()
# Bar chart positioning
ggplot2::ggplot(mpg, aes(x = manufacturer, fill = class)) +
ggplot2::geom_bar(colour = 'black')
# By default bars are "stacked" on top of each other. This makes comparing proportions rather difficult.
# You can control this with the position argument.
ggplot2::ggplot(mpg, aes(x = manufacturer, fill = class)) +
ggplot2::geom_bar(colour = 'black', position = 'dodge')
# Add a fitted line to a scatter plot
ggplot2::ggplot(mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::geom_smooth()
# Ensure the fitted line is a linear model
ggplot2::ggplot(mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point() +
ggplot2::geom_smooth(method = "lm")
# Histogram
ggplot2::ggplot(mpg, aes(x = hwy)) +
ggplot2::geom_histogram(bins = 30)
# Density plot
ggplot2::ggplot(mpg, aes(x = hwy)) +
ggplot2::geom_density()
# Overlapping density plots
ggplot2::ggplot(mpg, aes(x = hwy, fill = drv)) +
ggplot2::geom_density(alpha = 0.5)
# Box plots
ggplot2::ggplot(mpg, aes(x = drv, y = hwy)) +
ggplot2::geom_boxplot(fill = "red")
# Violin plots
ggplot2::ggplot(mpg, aes(x = drv, y = hwy)) +
ggplot2::geom_violin(aes(fill = drv))
# What is wrong with this plot?
ggplot2::ggplot(mpg, aes(x = cty, y = hwy)) +
ggplot2::geom_point()
# Because there are multiple observations, some are **overplotted**. To correct this, you can add
# some random noise to the data with `position = "jitter"` or `geom_jitter`.
ggplot2::ggplot(mpg, aes(x = cty, y = hwy)) +
ggplot2::geom_jitter(colour = 'red') +
ggplot2::geom_point()
# Line charts
ggplot2::economics |>
ggplot2::ggplot(aes(x = date, y = unemploy)) +
ggplot2::geom_line()
# Line charts
ggplot2::economics |>
ggplot2::ggplot(aes(x = date, y = unemploy)) +
ggplot2::geom_line() +
ggplot2::geom_line(aes(x = as.Date("2008-01-01")), col = "blue")
# Themes, titles, and multiple plots
ggplot2::ggplot(data = mpg, aes(x = class, y = ggplot2::after_stat(prop), group = 1)) +
ggplot2::geom_bar() +
ggplot2::labs(title = "Proportion of sample by class", x = "Class", y = "Proportion")
# Multiple plots
plot1 <- mpg |>
ggplot2::ggplot(aes(x = cyl, y = ggplot2::after_stat(prop), group = 1)) +
ggplot2::geom_bar(fill = "red") +
ggplot2::labs(title = "Proportion of sample by engine type",
x = "Number of cylinders",
y = "Proportion")
plot2 <- mpg |>
ggplot2::ggplot(aes(x = cty, y = hwy)) +
ggplot2::geom_jitter(colour = 'red') +
ggplot2::labs(title = "Fuel efficiency comparison",
subtitle = "Note: the points are jittered",
x = "City fuel efficiency",
y = "Highway fuel efficiency")
# gridExtra
gridExtra::grid.arrange(plot1, plot2, nrow = 1)
# Themes
gridExtra::grid.arrange(
plot2 + ggplot2::theme_bw(), plot2 + ggplot2::theme_classic(),
plot2 + ggplot2::theme_dark(), plot2 + ggplot2::theme_light())
# You can also make your own custom themes
ugly_theme <-
ggplot2::theme(
text = ggplot2::element_text(colour = 'orange', face = 'bold'),
panel.grid.major = ggplot2::element_line(colour = "violet", linetype = "dashed"),
panel.grid.minor = ggplot2::element_blank(),
panel.background = ggplot2::element_rect(fill = 'black', colour = 'red'),
)
plot2 + ugly_theme
# MoJ colour scheme
ggplot2::ggplot(mpg, aes(x = class, fill = drv)) +
ggplot2::geom_bar(position = "dodge") +
mojchart::scale_fill_moj(n = 3) +
mojchart::theme_gss(xlabel = TRUE)
# Accessible colour scheme
ggplot2::ggplot(mpg, aes(x = class, fill = drv)) +
ggplot2::geom_bar(position = "dodge") +
mojchart::scale_fill_moj(n = 3, scheme = "govanal_bars") +
mojchart::theme_gss(xlabel = TRUE)
## ggplot2::ggplot(mpg) +
## ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl)))
# Use ggplotly() to create a plotly interactive chart
plot <- ggplot2::ggplot(mpg, aes(x = displ, y = cty, colour = class)) +
ggplot2::geom_point()
plotly::ggplotly(plot)