-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-experimental.R
133 lines (117 loc) · 3.37 KB
/
06-experimental.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
# Load packages -----------------------------------------------------------
library(tidyverse)
library(tidycode)
library(treemapify)
library(ggfittext)
library(ggtext)
library(scales)
library(paletteer)
# List path of rscripts withing project directories -----------------------
rpaths <-
list.files(
path = "use/your/directory/path",
pattern = "*\\.R$",
recursive = TRUE,
full.names = TRUE
)
# Read rscripts as dataframe ----------------------------------------------
rcodes <-
map_dfr(rpaths, possibly(read_rfiles, otherwise = NULL))
# Classify function used in rscripts --------------------------------------
rcodes_class <-
rcodes %>%
filter(str_detect(file, "utils", negate = TRUE)) %>%
unnest_calls(expr) %>%
inner_join(
get_classifications(
lexicon = "crowdsource",
include_duplicates = TRUE
)
) %>%
anti_join(get_stopfuncs()) %>%
select(-args)
# Prepare data for visualization ------------------------------------------
to_plot <-
rcodes_class %>%
mutate(
classification = if_else(
func == "GET",
"import",
classification
),
classification = recode(
classification,
"data cleaning" = "transformation"
)
) %>%
count(classification) %>%
left_join(
treemapify(
.,
area = "n",
subgroup = "classification",
xlim = c(0, 10),
ylim = c(0, 10)
)
) %>%
mutate(
pct = n / sum(n),
label = percent(pct, accuracy = 0.1),
txtcolour = case_when(
pct < 0.1 ~ "#2e8db0",
TRUE ~ "#e5e5e3"
)
)
# Create plot -------------------------------------------------------------
p <-
to_plot %>%
ggplot(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)) +
treemapify:::geom_rrect(
aes(fill = pct),
radius = unit(15, "pt"),
colour = "#e5e5e3",
size = 5,
show.legend = FALSE
) +
geom_fit_text(
aes(label = label, colour = txtcolour),
place = "bottomright",
family = "Arial Narrow",
padding.x = unit(4, "mm"),
padding.y = unit(4, "mm"),
grow = TRUE
) +
geom_fit_text(
aes(label = classification, colour = txtcolour, angle = if_else(classification %in% c("export", "communication"), 0, 90)),
min.size = 10,
place = "topright",
family = "Arial Narrow",
fontface = "bold",
padding.x = unit(4, "mm"),
padding.y = unit(4, "mm"),
reflow = TRUE,
show.legend = FALSE
) +
labs(
caption = "<b style='font-size:35pt;color:grey15'>What are these codes for?</b><br>Classification of my #rstats codes within 25 data analysis projects at work<br><br><i style='font-size:10pt;'><br>﹋﹋﹋﹋﹋﹋﹋﹋﹋﹋<br>Data and visualization by Muhammad Aswan Syahputra</i>"
) +
scale_fill_paletteer_c("ggthemes::Blue-Teal") +
scale_colour_identity() +
theme_void(base_family = "Arial Narrow") +
theme(
plot.background = element_rect(fill = "#e5e5e3", colour = NA),
panel.background = element_rect(fill = "#e5e5e3", colour = NA),
plot.caption.position = "plot",
plot.caption = element_markdown(hjust = 0.5, colour = "gray25", size = rel(1.2), lineheight = 0.8),
plot.margin = margin(20, 20, 20, 20)
) +
coord_cartesian(clip = "off")
# Save plot ---------------------------------------------------------------
ggsave(
"outfile/06-experimental.png",
plot = p,
width = 8,
height = 8,
dpi = 300,
type = "cairo-png"
)