-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_ggplot_code.R
More file actions
238 lines (151 loc) · 4.78 KB
/
Copy pathbasic_ggplot_code.R
File metadata and controls
238 lines (151 loc) · 4.78 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
https://github.com/danieloc89/SWG-introduction-to-R/tree/main/sessions/session-4-plotting/data/wine-imports
install.packages("afcharts")
library(afcharts)
library(dplyr)
library(ggplot2)
library(janitor)
library(jsonlite)
library(readr)
library(readxl)
library(tidyr)
"C:\Users\tb000087\Downloads\May_2025_Over_10m_vessel_list.xlsx"
cereals_df <- read_csv("C:/Users/tb000087/Downloads/cereals (1).csv", name_repair = make_clean_names) |>
pivot_longer(
cols = -year,
names_to = "cereal",
values_to = "yield",
values_drop_na = TRUE
)
vessels_df <- read_xlsx("C:/Users/tb000087/Downloads/May_2025_Over_10m_vessel_list.xlsx", skip = 4, .name_repair = make_clean_names)
"C:\Users\tb000087\Downloads\May_2025_Over_10m_vessel_list.xlsx"
# UK GDP deflators series
gdp_deflators_df <- read_json("C:/Users/tb000087/Downloads/gdp-deflators (1).json", simplifyVector = TRUE)
# MMO vessel list
vessels_df <- read_xlsx("data/May_2025_Over_10m_vessel_list.xlsx", skip = 4, .name_repair = make_clean_names)
# scatter plot
plot(x = vessels_df$overall_length, y = vessels_df$vessel_capacity_units)
# line plot
plot(x = gdp_deflators_df$year, y = gdp_deflators_df$index, type = "l")
# Create the plot canvas
gdp_deflators_df |>
# apply aesthetics
ggplot(mapping = aes(x = year, y = pct_change))
# Create the plot canvas
gdp_deflators_df |>
ggplot(aes(x = year)) +
# create a line plot
geom_bar()
gdp_deflators_df |>
# create a variable to show if it is a forecast
mutate(forecast = if_else(year > 2024, "yes", "no")) |>
# apply this a mapping to distinguish between forecast and observed
ggplot(aes(x = year, y = pct_change, fill = forecast)) +
geom_col()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, fill = if_else(year > 2024, "yes", "no"))) +
geom_col()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, fill = if_else(pct_change > 3, "yes", "no"))) +
geom_col()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, color = if_else(year > 2020 ,"yes", "no"))) +
geom_line()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, color = if_else(year > 2020 ,"yes", "no"))) +
geom_line()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, linetype = if_else(year > 2020 ,"yes", "no"))) +
geom_line()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change)) +
geom_line()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, color = if_else(year > 2024, "yes", "no"))) +
geom_line() +
# applying lables to the plot
labs(
title = "Changes in the UK GDP Deflator Index",
subtitle = "From 1956 to 2029",
x = "Year",
y = "Percentage Change",
colour = "Forecast",
caption = "Source: HMT"
)
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, fill = if_else(year > 2024, "yes", "no"))) +
geom_col() +
labs(
title = "Changes in the UK GDP Deflator Index",
subtitle = "From 1956 to 2029",
x = NULL,
y = "Percentage Change",
fill = "Forecast",
caption = "Source: HMT"
) +
scale_fill_brewer(palette = "Set2")
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, fill = if_else(year > 2024, "yes", "no"))) +
geom_col() +
labs(
title = "Changes in the UK GDP Deflator Index",
subtitle = "From 1956 to 2029",
x = NULL,
y = "Percentage Change",
fill = "Forecast",
caption = "Source: HMT"
) +
scale_fill_manual(values = c(no = "cyan", yes = "red"))
afcharts::use_afcharts()
gdp_deflators_df |>
ggplot(aes(x = year, y = pct_change, fill = if_else(year > 2024, "yes", "no"))) +
geom_col() +
labs(
title = "Changes in the UK GDP Deflator Index",
subtitle = "From 1956 to 2029",
x = NULL,
y = "Percentage Change",
fill = "Forecast",
caption = "Source: HMT"
) +
scale_fill_discrete_af(scale_name = "Forecast") +
theme_af()
ggsave(
filename = "images/gdp-deflators.png",
width = 20,
height = 12,
units = "cm",
dpi = 400
)
cereals_df |>
ggplot(aes(x = year, y =yield, colour = cereal)) +
geom_line()
cereals_df |>
ggplot(aes(yield, fill = cereal)) +
geom_histogram(bins = 8)+
facet_wrap(vars(cereal))
library("ggplot2")
library("afcharts")
d <- subset(mpg, manufacturer == "ford")
d |>
ggplot(aes(x = model, y = hwy)) +
geom_line()
f <- data.frame(air
)
my_df <- data.frame(USAccDeaths)
my_df |>
ggplot(aes(x,y = AirPassengers)) +
geom_point()
USAccDeaths
my_df <- data.frame(
year = as.numeric(time(lynx)),
lynx = as.numeric(lynx)
)
my_df |> ggplot(aes(x = year, y = lynx)) +
geom_line() +
labs(title = "Annual Canadian Lynx Trappings", x = "Year", y = "Lynx Count")
library("ggplot2")
library("afcharts")
d <- subset(mpg, manufacturer == "ford")
ggplot(d, aes(x = class, fill = class)) +
geom_bar() +
scale_fill_discrete_af()