-
Notifications
You must be signed in to change notification settings - Fork 118
/
book_cover.Rmd
157 lines (139 loc) · 6.12 KB
/
book_cover.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
---
editor_options:
chunk_output_type: console
---
```{r, echo = FALSE, cache=FALSE, warning = FALSE, message=FALSE}
library(tibble)
library(dplyr)
library(purrr)
library(tidyr)
library(forcats)
library(ggplot2)
# import from file --------------------------------------------------------
df_long <- read.csv2("progress_data.csv", stringsAsFactors = FALSE,
header = TRUE) %>%
as_tibble() %>%
mutate(status = "finalised") %>%
modify_at("status", factor,
levels = c("open", "started", "solved", "reviewed", "finalised")) %>%
tibble::rowid_to_column("id") %>%
separate(chapter, "nr", sep = " ", remove = FALSE, extra = "drop") %>%
mutate(nr = as.integer(nr),
chapter = factor(chapter),
chapter = fct_reorder(chapter, nr, .desc = FALSE),
part = case_when(nr < 9 ~ "Foundations",
nr < 12 ~ "Functional\nProgramming",
nr < 17 ~ "Object Oriented\nProgramming",
nr < 22 ~ "Metaprogramming",
TRUE ~ "Techniques"),
part = factor(part, levels = c("Foundations", "Functional\nProgramming",
"Object Oriented\nProgramming",
"Metaprogramming", "Techniques"))) %>%
add_count(chapter)
# Intermediate calculations
percent_solved <- mean(df_long$status %in% c("solved", "reviewed", "finalised"), na.rm = TRUE) * 100
percent_reviewed <- mean(df_long$status %in% c("reviewed", "finalised"), na.rm = TRUE) * 100
```
## Bar Chart
```{r, echo = FALSE, cache=FALSE, warning = FALSE, message=FALSE, fig.width = 8, fig.height=10}
# Create Plot
plot_progress <- df_long %>%
group_by(chapter) %>%
mutate(exercise_in_chapter_id = row_number()) %>%
ungroup() %>%
ggplot(aes(x = chapter, fill = status, group = -id,
subchapter = subchapter, exercise = exercise,
credits = credits, alpha = -exercise_in_chapter_id^2)) +
geom_bar(color = "#FFFFFF", alpha = 0.8, aes(fill = chapter)) +
# geom_label(aes(y = n, label = n, color = chapter), fill = "white",
# nudge_y = 1.2, label.size = 0) +
# geom_text(aes(label = chapter), y = 40,
# color = "#555555", angle = 90, hjust = 1) +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_size = 14, base_family= "sans") +
theme(legend.position = "none",
# legend.justification = c(0, 1),
# strip.background = element_rect(fill = "white"),
strip.placement = "outside",
strip.background = element_blank(),
strip.text = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
# plot.title = element_text(hjust = 2.5),
# panel.spacing = unit(1, "lines"),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
coord_cartesian(ylim = c(0, 35)) +
guides(color = FALSE,
fill = guide_legend(nrow = 1)) +
viridis::scale_fill_viridis(discrete = TRUE, direction = 1, end = 0.95, option = "D") +
viridis::scale_color_viridis(discrete = TRUE, direction = 1, end = 0.95, option = "D") +
labs(#title = "Chapters and Exercises in Advanced R",
# subtitle = paste0("Reviewed: ",
# round(percent_reviewed, 1),
# "%"),
# caption = paste0("(Open: ",
# sum(df_long$status == "open"),
# " | Started: ",
# sum(df_long$status == "started"),
# " | Solved: ",
# sum(df_long$status %in% c("solved", "reviewed", "finalised")),
# " | Reviewed: ",
# sum(df_long$status %in% c("reviewed", "finalised")),
# " | Finalised: ",
# sum(df_long$status == "finalised"),
# ")"),
fill = "Status:") +
facet_grid(. ~ part, scales = "free",
space = "free", switch = "x")
plot_progress
```
Ideas
- tree map?
- make plot look nice in desired aspect ratio
- maybe put numbers of exercises in each chapter on top of bar?
- maybe use waffle chart?
- use a patch work arrangement of "multiple views on the data" (e.g. our progress over time? :D)
## Circle Packing
```{r}
library(ggraph)
library(igraph)
library(tidyverse)
library(viridis)
library(data.tree)
# Preprocess
df_nodes <- df_long %>%
select(part, chapter, subchapter, id) #%>%
# modify_if(is.factor, as.character) %>%
# add_count(part, name = "n_part") %>%
# add_count(part, chapter, name = "n_chapter") %>%
# add_count(part, chapter, subchapter, id, name = "n_subchapter")
df_nodes$pathString <- paste("Book",
df_nodes$part,
df_nodes$chapter,
df_nodes$subchapter,
df_nodes$id,
sep = "/")
# sum_n_recursively <- function(node) {
# node$n <-
# sum(c(node$n_subchapter, purrr::map_dbl(node$children, sum_n_recursively)), na.rm = TRUE)
# }
node_tree <- df_nodes %>%
as.Node()
# sum_n_recursively(node_tree)
mygraph <- node_tree %>%
as.igraph.Node(directed = TRUE)
# Make the graph
ggraph(mygraph, layout = 'circlepack') +
geom_node_circle(aes(fill = as.factor(depth), color = as.factor(depth)), alpha = .9) + #
scale_fill_manual(values=c("0" = "white", "1" = viridis(4)[1], "2" = viridis(4)[2], "3" = viridis(4)[3], "4"=viridis(4)[4])) +
scale_color_manual(values=c("0" = "white", "1" = viridis(4)[1], "2" = viridis(4)[2], "3" = viridis(4)[3], "4"=viridis(4)[4])) +
scale_color_manual(values=c("0" = "white", "1" = "black", "2" = "black", "3" = "black", "4"= "black")) +
theme_void() +
theme(legend.position="FALSE", plot.margin = unit(rep(0,4), "cm"))
# geom_node_label( aes(label = part_label), color = "white", size = 5, fill = alpha(c("white"), 0), label.size = 0) +
# geom_node_label( aes(label = chapter_label, color = as.factor(depth)), size = 5, fill = alpha(c("white"), 0), label.size = 0)
```