-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathValidateInput_plot.R
81 lines (73 loc) · 3.29 KB
/
ValidateInput_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
options(install.packages.check.source = "no")
packages = c("tidyverse", "showtext")
for(i in packages){
if(!require(i, character.only = T)){
# install.packages(i)
library(i, character.only = T)
}
}
plot_result <- function(result_file, outfile, fancy = TRUE){
final_result <- result_file %>%
filter (error == 0) %>%
dplyr::mutate(
name = paste(sheet_name, name, sep = " : "),
warning = if_else(severity=="warning", fails, 0L),
info = if_else(severity=="info", fails, 0L),
fails = if_else(severity=="error", fails, 0L)
) %>%
select(c("name","items","passes","warning","info","fails")) %>%
rowwise() %>%
dplyr::mutate(
items = sum(passes, warning, info, fails)
) %>%
pivot_longer(cols=c("passes","warning","info","fails"), names_to="result", values_to="total")
ylevel_order <- rev(final_result %>% select(name) %>% unique() %>% arrange(name) %>% .$name)
xlevel_order <- c("info", "fails", "warning", "passes")
# a normal plot...
if (!fancy){
p <- ggplot(final_result, aes(x=total/items, y=factor(name, level=ylevel_order), fill=factor(result, level=xlevel_order))) +
geom_bar(stat = "identity") +
xlab(NULL) + ylab(NULL) +
scale_fill_manual(values = c("passes"="#69d0be", "fails"="orangered", "warning"="darkgoldenrod1", "info"="dodgerblue")) +
scale_x_continuous(labels=scales::percent) +
scale_y_discrete(position = "right") +
geom_text(data=subset(final_result,total> 0), aes(label=total, y=name), size=3, position=position_fill(), hjust=1.4, color="white") +
ggtitle("Input Spreadsheet Validation Results") +
coord_cartesian(xlim=c(0,1)) +
theme(
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.margin=margin(3,1,3,3,"lines"),
legend.direction ="horizontal",
legend.position = "bottom",
legend.title=element_blank(),
panel.background=element_rect(fill = "white"),
text=element_text(size=11))
ggsave(outfile, p, width = 160, height = 160, units="mm")
}
else{
# a plot using fancy font...
font_add_google(name="Barlow Condensed", family="barlow-cond")
showtext_auto()
p <- ggplot(final_result, aes(x=total/items, y=factor(name, level=ylevel_order), fill=factor(result, level=xlevel_order))) +
geom_bar(stat = "identity", width=0.95) +
xlab(NULL) + ylab(NULL) +
scale_fill_manual(values = c("passes"="#69d0be", "fails"="orangered", "warning"="darkgoldenrod1", "info"="dodgerblue")) +
scale_x_continuous(labels=scales::percent) +
scale_y_discrete(position = "right") +
geom_text(data=subset(final_result,total> 0), aes(label=total, y=name), size=9, position=position_fill(), hjust=1.4, color="white") +
ggtitle("Input Spreadsheet Validation Results") +
coord_cartesian(xlim=c(0,1)) +
theme(
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.margin=margin(1,1,3,3,"lines"),
legend.direction ="horizontal",
legend.position = "bottom",
legend.title=element_blank(),
panel.background=element_rect(fill = "white"),
text=element_text(size=30, family="barlow-cond"))
ggsave(outfile, p, width = 160, height = 160, units="mm")
showtext_auto(FALSE)
}
}