generated from paulowiz/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board-games-lab.Rmd
337 lines (254 loc) · 9.63 KB
/
board-games-lab.Rmd
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
---
title: "R Notebook"
output: html_notebook
---
Run -\> *Ctrl+Shift+Enter*.
Add Cell -\> *Ctrl+Alt+I*.
*Preview on HTML -\> Ctrl+Shift+K*
## Reading Data
```{r}
library(tidytuesdayR)
tuesdata <- tidytuesdayR::tt_load('2022-01-25')
ratings <- tuesdata$ratings
details <- tuesdata$details
# Ensure 'id' columns are integers (or characters)
ratings$id <- as.integer(ratings$id)
details$id <- as.integer(details$id)
# Merge datasets on 'num' and 'id'
merged_data <- merge(ratings, details, by = c("id", "id"))
```
```{r}
head(ratings)
```
Columns Ratings Target : num, id, name, year, rank, average , Bayes_average, users_rated
Columns to drop : url
```{r}
head(details)
```
Columns to drop : description, boardgamefamily, boardgameexpansion, boardgameimplementation,
Not sure: boardgameartist, boardgamepublisher
Columns to clean: arrays -\> boardgamescategory, boardgamemecchanic, boardgamedesgin
```{r}
dim(ratings)
```
```{r}
colnames(ratings)
```
```{r}
colnames(details)
```
```{r}
# Load necessary libraries
library(dplyr)
library(ggplot2)
library(stringr)
# Assuming ratings and details datasets are already available in the environment
# Ensure 'id' columns are integers (or characters)
ratings$id <- as.integer(ratings$id)
details$id <- as.integer(details$id)
# Merge datasets on 'num' and 'id'
merged_data <- merge(ratings, details, by = c("id", "id"))
# Plot 1: Distribution of average ratings
plot1 <- ggplot(merged_data, aes(x = average)) +
geom_histogram(binwidth = 0.1, fill = "darkblue", color = "black") +
labs(title = "Distribution of Average Ratings",
x = "Average Rating",
y = "Frequency") +
theme_minimal()
# Plot 2: Average rating vs. number of users who rated
plot2 <- ggplot(merged_data, aes(x = users_rated, y = average)) +
geom_point(alpha = 0.5) +
labs(title = "Average Rating vs. Number of Users Rated",
x = "Number of Users Rated",
y = "Average Rating") +
theme_minimal() +
scale_x_log10(labels = scales::trans_format("log10", scales::math_format(10^.x)))
# scale_x_log10() # log scale for better visualization
# Plot 3: Distribution of games published per year
plot3 <- ggplot(merged_data, aes(x = yearpublished)) +
geom_histogram(binwidth = 1,fill = "darkblue", color = "black") +
labs(title = "Distribution of Games Published Per Year",
x = "Year Published",
y = "Number of Games") +
theme_minimal()
# Plot 4: Average rating by category
# Since a game can have multiple categories, we need to handle that
library(tidyr)
category_data <- merged_data %>%
separate_rows(boardgamecategory, sep = ",") %>%
mutate(boardgamecategory = str_replace_all(boardgamecategory, "\\[|\\]", "")) %>%
group_by(boardgamecategory) %>%
summarise(average_rating = mean(average, na.rm = TRUE),
count = n()) %>%
filter(count > 5) # Filter to show categories with more than 5 games
# Print the plots
print(plot1)
print(plot2)
print(plot3)
```
```{r}
# Number of ratings per board game
num_ratings_per_game <- merged_data %>%
select(name, users_rated) %>%
arrange(desc(users_rated))
# Number of ratings per board game with average rating
num_ratings_per_game <- merged_data %>%
select(name, users_rated, average) %>%
arrange(desc(users_rated))
# Plot the number of ratings per game (top 20 for readability) with average ratings
plot_ratings_per_game <- ggplot(head(num_ratings_per_game, 20), aes(x = reorder(name, users_rated), y = users_rated)) +
geom_col(fill = "darkblue") +
geom_text(aes(label = round(average, 2)), hjust = -0.2, color = "black", size = 3.5) +
labs(title = "Top 20 Board Games by Number of Ratings",
x = "Board Game",
y = "Number of Ratings") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
# Print the plot
print(plot_ratings_per_game)
# Save the plot to a file with increased width
ggsave("plot_ratings_per_game.png", plot = plot_ratings_per_game, width = 12, height = 8)
```
```{r}
# Plot the number of ratings per game (top 20 for readability) with average ratings
plot_ratings_per_game <- ggplot(head(num_ratings_per_game, 20), aes(x = reorder(name, average), y = users_rated)) +
geom_col(fill = "darkgreen") +
geom_text(aes(label = round(average, 2)), hjust = -0.2, color = "black", size = 3.5) +
labs(title = "Top 20 Board Games by Average Rating",
x = "Board Game",
y = "Number of Ratings") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
# Print the plot
print(plot_ratings_per_game)
```
```{r}
category_data <- merged_data %>%
separate_rows(boardgamemechanic, sep = ",") %>%
mutate(boardgamemechanic = str_replace_all(boardgamemechanic, "\\[|\\]", "")) %>%
group_by(boardgamemechanic) %>%
summarise(average_rating = mean(average, na.rm = TRUE),
count = n()) %>%
filter(count > 5) # Filter to show categories with more than 5 games
plot5 <- ggplot(head(category_data,20), aes(x = reorder(boardgamemechanic, average_rating), y = average_rating)) +
geom_col(fill = "purple") +
labs(title = "Average Rating by Mechanic",
x = "Mechanic",
y = "Average Rating") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
print(plot5)
```
```{r}
category_data <- merged_data %>%
separate_rows(boardgamefamily, sep = ",") %>%
mutate(boardgamefamily = str_replace_all(boardgamefamily, "\\[|\\]", "")) %>%
group_by(boardgamefamily) %>%
summarise(average_rating = mean(average, na.rm = TRUE),
count = n()) %>%
filter(count > 5) # Filter to show categories with more than 5 games
plot5 <- ggplot(head(category_data,20), aes(x = reorder(boardgamefamily, average_rating), y = average_rating)) +
geom_col(fill = "purple") +
labs(title = "Average Rating by Family",
x = "Family",
y = "Average Rating") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
print(plot5)
```
```{r}
category_data <- merged_data %>%
separate_rows(boardgameexpansion, sep = ",") %>%
mutate(boardgameexpansion = str_replace_all(boardgameexpansion, "\\[|\\]", "")) %>%
group_by(boardgameexpansion) %>%
summarise(average_rating = mean(average, na.rm = TRUE),
count = n()) %>%
filter(count > 5) # Filter to show categories with more than 5 games
plot7 <- ggplot(head(category_data,20), aes(x = reorder(boardgameexpansion, average_rating), y = average_rating)) +
geom_col(fill = "purple") +
labs(title = "Average Rating by Implementation",
x = "Implementation",
y = "Average Rating") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
print(plot7)
```
```{r}
category_data <- merged_data %>%
separate_rows(boardgamedesigner, sep = ",") %>%
mutate(boardgamedesigner = str_replace_all(boardgamedesigner, "\\[|\\]", "")) %>%
group_by(boardgamedesigner) %>%
summarise(average_rating = mean(average, na.rm = TRUE),
count = n()) %>%
filter(count > 5) # Filter to show categories with more than 5 games
plot7 <- ggplot(head(category_data,20), aes(x = reorder(boardgamedesigner, average_rating), y = average_rating)) +
geom_col(fill = "purple") +
labs(title = "Average Rating by Designer",
x = "Designer",
y = "Average Rating") +
theme_minimal() +
coord_flip() # Flip coordinates for better readability
print(plot7)
```
```{r}
# Load necessary libraries
library(ggplot2)
library(reshape2)
# Select only numeric columns
numeric_data <- merged_data %>% select(where(is.numeric))
# Calculate the correlation matrix
correlation_matrix <- cor(numeric_data)
# Melt the correlation matrix into a long format
melted_corr_matrix <- melt(correlation_matrix)
# Create the correlation heatmap with annotations
heatmap <- ggplot(melted_corr_matrix, aes(x = Var1, y = Var2, fill = value)) +
geom_tile(color = "white") +
scale_fill_gradient2(low = "red", high = "green", mid = "lightyellow",
midpoint = 0, limit = c(-1, 1), space = "Lab",
name = "Correlation") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
labs(title = "Correlation Heatmap",
x = "Variables",
y = "Variables") +
geom_text(aes(label = round(value, 2)), color = "black", size = 3)
# Display the heatmap
print(heatmap)
```
```{r}
# Load necessary libraries
library(ggplot2)
# Assuming 'merged_data' contains the necessary columns 'average' and 'users_rated'
# Let's plot 'average' rating against 'users_rated'
# Scatter plot
plot <- ggplot(merged_data, aes(x = users_rated, y = average)) +
geom_point(alpha = 0.5, color = "blue") + # Add points with transparency
labs(title = "Average Rating vs. Number of Reviews",
x = "Number of Reviews (Users Rated)",
y = "Average Rating") + # Add labels
theme_minimal() # Apply a minimal theme
# Display the plot
print(plot)
```
```{r}
library(dplyr)
library(tidyr)
library(ggplot2)
# Clean and split the categories
clean_data <- merged_data %>%
mutate(boardgamecategory = gsub("\\[|\\]|'", "", boardgamecategory)) %>%
separate_rows(boardgamecategory, sep = ", ") %>%
filter(boardgamecategory != "")
# Count the occurrences of each category
category_distribution <- clean_data %>%
count(boardgamecategory) %>%
arrange(desc(n))
# Select the top 20 categories
top_category_distribution <- head(category_distribution, 40)
print(category_distribution)
# Plot the distribution
ggplot(top_category_distribution, aes(x = reorder(boardgamecategory, -n), y = n)) +
geom_bar(stat = "identity", fill = "skyblue") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Distribution of Board Game Categories Top 40 from 84 categories", x = "Category", y = "Frequency")
````