Skip to content

Commit d59ce4c

Browse files
authored
Merge pull request #405 from UBC-DSCI/plot-font
Plot font sizes
2 parents d2bdfbd + bbd0e0f commit d59ce4c

File tree

8 files changed

+125
-77
lines changed

8 files changed

+125
-77
lines changed

classification1.Rmd

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ library(knitr)
77
library(kableExtra)
88
library(ggpubr)
99
library(stringr)
10+
library(ggplot2)
1011
1112
knitr::opts_chunk$set(echo = TRUE,
1213
fig.align = "center")
@@ -31,6 +32,8 @@ print_tidymodels <- function(tidymodels_object) {
3132
}
3233
}
3334
}
35+
36+
theme_update(axis.title = element_text(size = 12)) # modify axis label size in plots
3437
```
3538

3639
## Overview
@@ -236,7 +239,8 @@ perim_concav <- cancer |>
236239
y = "Concavity (standardized)",
237240
color = "Diagnosis") +
238241
scale_color_manual(labels = c("Malignant", "Benign"),
239-
values = c("orange2", "steelblue2"))
242+
values = c("orange2", "steelblue2")) +
243+
theme(text = element_text(size = 12))
240244
perim_concav
241245
```
242246

@@ -613,9 +617,9 @@ scaled_cancer_3 <- bind_rows(cancer, new_obs_3) |>
613617
plot_3d <- scaled_cancer_3 |>
614618
plot_ly() |>
615619
layout(scene = list(
616-
xaxis = list(title = "Perimeter"),
617-
yaxis = list(title = "Concavity"),
618-
zaxis = list(title = "Symmetry")
620+
xaxis = list(title = "Perimeter", titlefont = list(size = 14)),
621+
yaxis = list(title = "Concavity", titlefont = list(size = 14)),
622+
zaxis = list(title = "Symmetry", titlefont = list(size = 14))
619623
)) |>
620624
add_trace(x = ~Perimeter,
621625
y = ~Concavity,
@@ -1057,7 +1061,7 @@ ggplot(unscaled_cancer, aes(x = Area,
10571061
facet_zoom(x = ( Area > 380 & Area < 420) ,
10581062
y = (Smoothness > 0.08 & Smoothness < 0.14), zoom.size = 2) +
10591063
theme_bw() +
1060-
theme(text = element_text(size = 14), legend.position="bottom")
1064+
theme(text = element_text(size = 18), axis.title=element_text(size=18), legend.position="bottom")
10611065
```
10621066

10631067
### Balancing
@@ -1103,7 +1107,8 @@ rare_plot <- rare_cancer |>
11031107
y = "Concavity (standardized)",
11041108
color = "Diagnosis") +
11051109
scale_color_manual(labels = c("Malignant", "Benign"),
1106-
values = c("orange2", "steelblue2"))
1110+
values = c("orange2", "steelblue2")) +
1111+
theme(text = element_text(size = 12))
11071112
11081113
rare_plot
11091114
```
@@ -1398,7 +1403,8 @@ wkflw_plot <-
13981403
x = "Area (standardized)",
13991404
y = "Smoothness (standardized)") +
14001405
scale_color_manual(labels = c("Malignant", "Benign"),
1401-
values = c("orange2", "steelblue2"))
1406+
values = c("orange2", "steelblue2")) +
1407+
theme(text = element_text(size = 12))
14021408
14031409
wkflw_plot
14041410
```

classification2.Rmd

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library(gridExtra)
55
library(cowplot)
66
library(stringr)
77
library(knitr)
8+
library(ggplot2)
89
910
knitr::opts_chunk$set(fig.align = "center")
1011
@@ -24,6 +25,9 @@ print_tidymodels <- function(tidymodels_object) {
2425
}
2526
}
2627
}
28+
29+
theme_update(axis.title = element_text(size = 12)) # modify axis label size in plots
30+
2731
```
2832

2933
## Overview
@@ -227,7 +231,8 @@ perim_concav <- cancer |>
227231
geom_point(alpha = 0.5) +
228232
labs(color = "Diagnosis") +
229233
scale_color_manual(labels = c("Malignant", "Benign"),
230-
values = c("orange2", "steelblue2"))
234+
values = c("orange2", "steelblue2")) +
235+
theme(text = element_text(size = 12))
231236
232237
perim_concav
233238
```
@@ -782,7 +787,8 @@ as shown in Figure \@ref(fig:06-find-k).
782787
accuracy_vs_k <- ggplot(accuracies, aes(x = neighbors, y = mean)) +
783788
geom_point() +
784789
geom_line() +
785-
labs(x = "Neighbors", y = "Accuracy Estimate")
790+
labs(x = "Neighbors", y = "Accuracy Estimate") +
791+
theme(text = element_text(size = 12))
786792
787793
accuracy_vs_k
788794
```
@@ -839,7 +845,8 @@ accuracies <- knn_results |>
839845
accuracy_vs_k_lots <- ggplot(accuracies, aes(x = neighbors, y = mean)) +
840846
geom_point() +
841847
geom_line() +
842-
labs(x = "Neighbors", y = "Accuracy Estimate")
848+
labs(x = "Neighbors", y = "Accuracy Estimate") +
849+
theme(text = element_text(size = 12))
843850
844851
accuracy_vs_k_lots
845852
```
@@ -919,7 +926,7 @@ for (i in 1:length(ks)) {
919926
ggtitle(paste("K = ", ks[[i]])) +
920927
scale_color_manual(labels = c("Malignant", "Benign"),
921928
values = c("orange2", "steelblue2")) +
922-
theme(text = element_text(size = 18))
929+
theme(text = element_text(size = 18), axis.title=element_text(size=18))
923930
}
924931
925932
p_no_legend <- lapply(plots, function(x) x + theme(legend.position = "none"))
@@ -1029,7 +1036,7 @@ variables there are, the more (random) influence they have, and the more they
10291036
corrupt the set of nearest neighbors that vote on the class of the new
10301037
observation to predict.
10311038

1032-
```{r 06-performance-irrelevant-features, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "60%", fig.cap = "Effect of inclusion of irrelevant predictors."}
1039+
```{r 06-performance-irrelevant-features, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "65%", fig.cap = "Effect of inclusion of irrelevant predictors."}
10331040
# get accuracies after including k irrelevant features
10341041
ks <- c(0, 5, 10, 15, 20, 40)
10351042
fixedaccs <- list()
@@ -1103,7 +1110,7 @@ plt_irrelevant_accuracies <- ggplot(res) +
11031110
geom_line(mapping = aes(x=ks, y=accs)) +
11041111
labs(x = "Number of Irrelevant Predictors",
11051112
y = "Model Accuracy Estimate") +
1106-
theme(text = element_text(size = 18))
1113+
theme(text = element_text(size = 18), axis.title=element_text(size=18))
11071114
11081115
plt_irrelevant_accuracies
11091116
```
@@ -1119,12 +1126,12 @@ variables, the number of neighbors does not increase smoothly; but the general t
11191126
Figure \@ref(fig:06-fixed-irrelevant-features) corroborates
11201127
this evidence; if we fix the number of neighbors to $K=3$, the accuracy falls off more quickly.
11211128

1122-
```{r 06-neighbors-irrelevant-features, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "60%", fig.cap = "Tuned number of neighbors for varying number of irrelevant predictors."}
1129+
```{r 06-neighbors-irrelevant-features, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "65%", fig.cap = "Tuned number of neighbors for varying number of irrelevant predictors."}
11231130
plt_irrelevant_nghbrs <- ggplot(res) +
11241131
geom_line(mapping = aes(x=ks, y=nghbrs)) +
11251132
labs(x = "Number of Irrelevant Predictors",
11261133
y = "Number of neighbors") +
1127-
theme(text = element_text(size = 18))
1134+
theme(text = element_text(size = 18), axis.title=element_text(size=18))
11281135
11291136
plt_irrelevant_nghbrs
11301137
```
@@ -1138,7 +1145,7 @@ plt_irrelevant_nghbrs <- ggplot(res_tmp) +
11381145
geom_line(mapping = aes(x=ks, y=accuracy, color=Type)) +
11391146
labs(x = "Number of Irrelevant Predictors", y = "Accuracy") +
11401147
scale_color_discrete(labels= c("Tuned K", "K = 3")) +
1141-
theme(text = element_text(size = 16))
1148+
theme(text = element_text(size = 17), axis.title=element_text(size=17))
11421149
11431150
plt_irrelevant_nghbrs
11441151
```
@@ -1366,12 +1373,12 @@ where the elbow occurs, and whether adding a variable provides a meaningful incr
13661373
> part of tuning your classifier, you *cannot use your test data* for this
13671374
> process!
13681375
1369-
```{r 06-fwdsel-3, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "60%", fig.cap = "Estimated accuracy versus the number of predictors for the sequence of models built using forward selection."}
1376+
```{r 06-fwdsel-3, echo = FALSE, warning = FALSE, fig.retina = 2, out.width = "65%", fig.cap = "Estimated accuracy versus the number of predictors for the sequence of models built using forward selection."}
13701377
fwd_sel_accuracies_plot <- accuracies |>
13711378
ggplot(aes(x = size, y = accuracy)) +
13721379
geom_line() +
13731380
labs(x = "Number of Predictors", y = "Estimated Accuracy") +
1374-
theme(text = element_text(size = 18))
1381+
theme(text = element_text(size = 20), axis.title=element_text(size=20))
13751382
13761383
fwd_sel_accuracies_plot
13771384
```

clustering.Rmd

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ knitr::opts_chunk$set(warning = FALSE, fig.align = "default")
1616
# some graphs with the code shown to students are hard coded
1717
cbbPalette <- c(brewer.pal(9, "Paired"))
1818
cbpalette <- c("darkorange3", "dodgerblue3", "goldenrod1")
19+
20+
theme_update(axis.title = element_text(size = 12)) # modify axis label size in plots
1921
```
2022

2123
## Overview
@@ -171,7 +173,8 @@ ggplot(data, aes(x = flipper_length_standardized,
171173
y = bill_length_standardized)) +
172174
geom_point() +
173175
xlab("Flipper Length (standardized)") +
174-
ylab("Bill Length (standardized)")
176+
ylab("Bill Length (standardized)") +
177+
theme(text = element_text(size = 12))
175178
```
176179

177180
Based \index{ggplot}\index{ggplot!geom\_point} on the visualization
@@ -470,7 +473,7 @@ for (i in 1:4) {
470473
color = "black",
471474
fill = cbpalette) +
472475
annotate("text", x = -0.5, y = 1.5, label = paste0("Iteration ", i), size = 5)+
473-
theme(text = element_text(size = 14))
476+
theme(text = element_text(size = 14), axis.title=element_text(size=14))
474477
475478
if (i == 1 | i == 2) {
476479
plt_ctr <- plt_ctr +
@@ -503,8 +506,8 @@ for (i in 1:4) {
503506
stroke = 1,
504507
color = "black",
505508
fill = cbpalette) +
506-
annotate("text", x = -0.5, y = 1.5, label = paste0("Iteration ", i), size = 5)+
507-
theme(text = element_text(size = 14))
509+
annotate("text", x = -0.5, y = 1.5, label = paste0("Iteration ", i), size = 5) +
510+
theme(text = element_text(size = 14), axis.title=element_text(size=14))
508511
509512
if (i == 1 | i ==2) {
510513
plt_lbl <- plt_lbl +
@@ -643,7 +646,7 @@ for (i in 1:5) {
643646
color = "black",
644647
fill = cbpalette) +
645648
annotate("text", x = -0.5, y = 1.5, label = paste0("Iteration ", i), size = 5) +
646-
theme(text = element_text(size = 14))
649+
theme(text = element_text(size = 14), axis.title=element_text(size=14))
647650
648651
if (i == 1 | i == 2) {
649652
plt_ctr <- plt_ctr +
@@ -676,7 +679,7 @@ for (i in 1:5) {
676679
color = "black",
677680
fill = cbpalette) +
678681
annotate("text", x = -0.5, y = 1.5, label = paste0("Iteration ", i), size = 5) +
679-
theme(text = element_text(size = 14))
682+
theme(text = element_text(size = 14), axis.title=element_text(size=14))
680683
681684
if (i == 1 | i == 2) {
682685
plt_lbl <- plt_lbl +
@@ -726,15 +729,15 @@ ggarrange(iter_plot_list[[1]] +
726729
theme(axis.text.x = element_blank(),
727730
axis.ticks.x = element_blank(),
728731
axis.title.x = element_blank(),
729-
plot.margin = margin(r = 2, t = 2, b = 1)),
732+
plot.margin = margin(r = 2, t = 2, b = 2)),
730733
iter_plot_list[[6]] +
731734
theme(axis.text.y = element_blank(),
732735
axis.ticks.y = element_blank(),
733736
axis.title.y = element_blank(),
734737
axis.text.x = element_blank(),
735738
axis.ticks.x = element_blank(),
736739
axis.title.x = element_blank(),
737-
plot.margin = margin(r = 2, l = 2, t = 2, b = 1) ),
740+
plot.margin = margin(r = 2, l = 2, t = 2, b = 2) ),
738741
iter_plot_list[[7]] +
739742
theme(axis.text.y = element_blank(),
740743
axis.ticks.y = element_blank(),
@@ -823,7 +826,9 @@ p1 <- ggplot(assignments, aes(flipper_length_standardized,
823826
size = 4,
824827
shape = 21,
825828
stroke = 1) +
826-
scale_fill_manual(values = cbbPalette)
829+
scale_fill_manual(values = cbbPalette) +
830+
theme(text = element_text(size = 12), axis.title=element_text(size=12))
831+
827832
828833
p1
829834
```
@@ -938,7 +943,8 @@ cluster_plot <- ggplot(clustered_data,
938943
color = "Cluster") +
939944
scale_color_manual(values = c("dodgerblue3",
940945
"darkorange3",
941-
"goldenrod1"))
946+
"goldenrod1")) +
947+
theme(text = element_text(size = 12))
942948
943949
cluster_plot
944950
```
@@ -1040,7 +1046,8 @@ elbow_plot <- ggplot(clustering_statistics, aes(x = k, y = tot.withinss)) +
10401046
geom_line() +
10411047
xlab("K") +
10421048
ylab("Total within-cluster sum of squares") +
1043-
scale_x_continuous(breaks = 1:9)
1049+
scale_x_continuous(breaks = 1:9) +
1050+
theme(text = element_text(size = 12))
10441051
10451052
elbow_plot
10461053
```
@@ -1082,7 +1089,8 @@ elbow_plot <- ggplot(clustering_statistics, aes(x = k, y = tot.withinss)) +
10821089
geom_line() +
10831090
xlab("K") +
10841091
ylab("Total within-cluster sum of squares") +
1085-
scale_x_continuous(breaks = 1:9)
1092+
scale_x_continuous(breaks = 1:9) +
1093+
theme(text = element_text(size = 12))
10861094
10871095
elbow_plot
10881096
```

0 commit comments

Comments
 (0)