-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_3D_plot.R
248 lines (218 loc) · 6.76 KB
/
make_3D_plot.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
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
library(jsonlite)
library(dplyr)
# "AllPrintings" from mtgjson.com.
mtg_cards <- fromJSON(readLines("AllCards.json"))
columns_of_interest = c(
"Name",
"White",
"Blue",
"Black",
"Red",
"Green",
"Mana Cost",
"Type",
"Power",
"Toughness")
rows = length(mtg_cards)
columns = length(columns_of_interest)
mtg_matrix <- matrix(ncol = columns, nrow = rows)
colnames(mtg_matrix) <- columns_of_interest
# Iterate over JSON file to make a matrix
i = 1
for (card in mtg_cards) {
# Skip the "joke" sets
if(!"UGL" %in% card$printings && !"UNH" %in% card$printings && !"UST" %in% card$printings) {
mtg_matrix[i,"Name"] <- card$name
id <- card$colorIdentity
if("W" %in% id) {
mtg_matrix[i,"White"] <- 1
} else {
mtg_matrix[i,"White"] <- 0
}
if("U" %in% id) {
mtg_matrix[i,"Blue"] <- 1
} else {
mtg_matrix[i,"Blue"] <- 0
}
if("B" %in% id) {
mtg_matrix[i,"Black"] <- 1
} else {
mtg_matrix[i,"Black"] <- 0
}
if("R" %in% id) {
mtg_matrix[i,"Red"] <- 1
} else {
mtg_matrix[i,"Red"] <- 0
}
if("G" %in% id) {
mtg_matrix[i,"Green"] <- 1
} else {
mtg_matrix[i,"Green"] <- 0
}
mtg_matrix[i, "Mana Cost"] <- as.integer(card$convertedManaCost)
mtg_matrix[i, "Type"] <- card$types[1]
if(!is.null(card$power)) {
mtg_matrix[i, "Power"] <- card$power
}
if(!is.null(card$toughness)) {
mtg_matrix[i, "Toughness"] <- card$toughness
}
}
i = i + 1
}
# Remove empty rows
mtg_matrix <- mtg_matrix[rowSums(is.na(mtg_matrix)) != ncol(mtg_matrix), ]
# Make it a dataframe
mtg_df <- data.frame(mtg_matrix, stringsAsFactors = FALSE)
mtg_df$White <- as.numeric(as.character(mtg_df$White))
mtg_df$Blue <- as.numeric(as.character(mtg_df$Blue))
mtg_df$Black <- as.numeric(as.character(mtg_df$Black))
mtg_df$Red <- as.numeric(as.character(mtg_df$Red))
mtg_df$Green <- as.numeric(as.character(mtg_df$Green))
mtg_df$Mana.Cost <- as.numeric(as.character(mtg_df$Mana.Cost))
mtg_df$Power <- as.numeric(as.character(mtg_df$Power))
mtg_df$Toughness <- as.numeric(as.character(mtg_df$Toughness))
mtg_df$Times.Printed <- as.numeric(as.character(mtg_df$Times.Printed))
sapply(mtg_df, mode)
### Make new dataset for 3D plotting!
# Check if card is monocolored or colorless.
monocolor <- function(card) {
color_count <- sum(card[,c("White", "Blue", "Black", "Red", "Green")])
return(color_count == 1 || color_count == 0)
}
# If a card is monocolored/colorless, return its color ("Yellow" for colorless).
which_color <- function(card) {
if(card$White == 1) {
return("White")
}
if(card$Blue == 1) {
return("Blue")
}
if(card$Black == 1) {
return("Black")
}
if(card$Red == 1) {
return("Red")
}
if(card$Green == 1) {
return("DarkGreen")
}
return("Yellow")
}
# List of monocolor/colorless-indexes.
is_it_monocolor <- c()
for (i in 1:nrow(mtg_df)) {
is_it_monocolor <- c(is_it_monocolor, monocolor(mtg_df[i,]))
}
# New dataset of only monocolor/colorless cards, labeled with color.
mtg_monocolor <- mtg_df[is_it_monocolor,]
which_monocolor <- c()
for (i in 1:nrow(mtg_monocolor)) {
which_monocolor <- c(which_monocolor, which_color(mtg_monocolor[i,]))
}
mtg_monocolor$Color <- which_monocolor
# Table the data to plot frequencies.
cpt_table <- table(mtg_monocolor[,c("Power", "Toughness", "Mana.Cost", "Color")])
cpt_df <- as.data.frame(cpt_table)
# Dummy-data so make a little legend.
legend_dummies <- data.frame(
c(15,15,15,15),
c(15,15,15,15),
c(5,4.8,4.5,4.1),
c("Yellow", "Yellow", "Yellow", "Yellow"),
c(1,5,50,500)
)
names(legend_dummies) <- c("Power", "Toughness", "Mana.Cost", "Color", "Freq")
cpt_df <- rbind(legend_dummies, cpt_df)
# Take logarithm of frequencies.
cpt_df$Power <- as.numeric(as.character(cpt_df$Power))
cpt_df$Toughness <- as.numeric(as.character(cpt_df$Toughness))
cpt_df$Mana.Cost <- as.numeric(as.character(cpt_df$Mana.Cost))
extra <- 2
cpt_df$log_Freq <- log(cpt_df$Freq + extra)
cpt_df$log_Freq[cpt_df$Freq == 0] <- 0
# Bump power/toughness by color to make pentagons.
for (i in 1:nrow(cpt_df)) {
if(cpt_df[i,"Color"] == "White") {
edit_p <- 0
edit_t <- 1
} else if(cpt_df[i,"Color"] == "Blue") {
edit_p <- .95
edit_t <- .3
} else if(cpt_df[i,"Color"] == "Black") {
edit_p <- .6
edit_t <- -.8
} else if(cpt_df[i,"Color"] == "Red") {
edit_p <- -.6
edit_t <- -.8
} else if(cpt_df[i,"Color"] == "DarkGreen"){
edit_p <- -.95
edit_t <- .3
} else {
edit_p <- 0
edit_t <- 0
}
edit_p <- edit_p * .25
edit_t <- edit_t * .25
cpt_df[i,"Power"] = cpt_df[i,"Power"] + edit_p
cpt_df[i,"Toughness"] = cpt_df[i,"Toughness"] + edit_t
}
##### Now plot it in 3D
library(rgl)
# Nice axes-function from http://www.sthda.com/english/wiki/a-complete-guide-to-3d-visualization-device-system-in-r-r-software-and-data-visualization
rgl_add_axes <- function(x, y, z, axis.cols = c("grey", "grey", "grey"),
xlab = "", ylab="", zlab="", show.plane = TRUE,
show.bbox = FALSE, bbox.col = c("#333377","black"))
{
lim <- function(x){c(0, max(abs(x))) * 1.1}
# Add axes
xlim <- lim(x); ylim <- lim(y); zlim <- lim(z)
rgl.lines(xlim, c(0, 0), c(0, 0), color = axis.cols[1])
rgl.lines(c(0, 0), ylim, c(0, 0), color = axis.cols[2])
rgl.lines(c(0, 0), c(0, 0), zlim, color = axis.cols[3])
# Add a point at the end of each axes to specify the direction
axes <- rbind(c(xlim[2], 0, 0), c(0, ylim[2], 0),
c(0, 0, zlim[2]))
rgl.points(axes, color = "black", size = 3)
# Add axis labels
rgl.texts(axes, text = c(xlab, ylab, zlab), color = "black",
adj = c(0.5, -0.8), size = 2)
}
open3d()
rgl_add_axes(
cpt_df$Power,
cpt_df$Toughness,
cpt_df$Mana.Cost,
xlab = "Power",
ylab = "Toughness",
zlab = "Mana Cost",
show.plane = FALSE,
axis.cols = c("Yellow", "Yellow", "Black")
)
axis3d('x', pos=c( NA, 0, 0 ), col = "Yellow")
axis3d('y', pos=c( 0, NA, 0 ), col = "Yellow")
axis3d('z', pos=c( 0, 0, NA ), col = "Black")
for (i in 1:length(cpt_df$Power)) {
if(cpt_df$log_Freq[i] != 0) {
shade3d(
translate3d(
x = cpt_df$Power[i],
y = cpt_df$Toughness[i],
z = cpt_df$Mana.Cost[i],
obj = scale3d(
x = .03*cpt_df$log_Freq[i],
y = .03*cpt_df$log_Freq[i],
z = .03*cpt_df$log_Freq[i],
obj = icosahedron3d(col = cpt_df$Color[i])
)
)
)
}
}
### Export 3D model as PLY-file
writePLY(
"RGL.ply",
withColors = TRUE,
format = "little_endian",
pointRadius = .001
)