forked from arielf/weight-loss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score-chart.r
executable file
·136 lines (121 loc) · 3.82 KB
/
score-chart.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
#!/usr/bin/Rscript --vanilla
#
# Generate weight gain/loss factor chart
#
library(ggplot2)
eprintf <- function(...) cat(sprintf(...), sep='', file=stderr())
# --- styles
ratio = 1.61803398875
W = 10
H = W * ratio
DPI = 200
FONTSIZE = 8
MyGray = 'grey50'
# --- Favorite fonts
Family='FreeSans'
Face='bold.italic'
title.theme = element_text(family=Family, face=Face,
size=FONTSIZE)
x.title.theme = element_text(family=Family, face=Face,
size=FONTSIZE-1, vjust=-0.1)
y.title.theme = element_text(family=Family, face=Face,
size=FONTSIZE-1, angle=90, vjust=0.2)
x.axis.theme = element_text(family=Family, face="bold",
size=FONTSIZE-2, colour=MyGray)
y.axis.theme = element_text(family=Family, face="bold",
size=FONTSIZE-2, colour=MyGray)
legend.theme = element_text(family=Family, face=Face,
size=FONTSIZE-1, colour="black")
Params <- list()
process.args <- function() {
argv <- commandArgs(trailingOnly = TRUE)
fileArgs <- c()
for (arg in argv) {
# Arguments can be either:
# Params: name=value
# or:
# Files: file arguments
# eprintf("arg: %s\n", arg)
var.val <- unlist(strsplit(arg, '='))
if (length(var.val) == 2) {
var <- var.val[1]
val <- var.val[2]
Params[[var]] <<- val
# eprintf('Params$%s=%s\n', var, val)
} else {
fileArgs <- c(fileArgs, arg)
}
}
# for (n in names(Params)) {
# eprintf("Params[[%s]]: %s\n", n, Params[[n]]);
# }
# Params are assigned to global array Params[]
# rest are returned as files
fileArgs
}
# --- main
FileArgs <- process.args()
CsvFile <- ifelse(
length(FileArgs) > 0 && nchar(FileArgs[1]) > 0,
FileArgs[1],
'scores.csv'
)
PngFile <- ifelse(
length(FileArgs) > 1 && nchar(FileArgs[2]) > 0,
FileArgs[2],
gsub(CsvFile, pattern='.[tc]sv', replacement='.png')
)
Title <- ifelse(length(Params$title),
Params$title,
'Relative weight-loss factor importance\n(negative/green means causing weight-loss\npositive/red means causing weight-gain)'
)
# -- Color weight-gains in red and weigh-losses in green for effect
# (this is one uncommon case where a 'positive' quantity is
# actually undesired/negative)
MyGreen = '#00cc00'
MyRed = '#ff0000'
d <- read.csv(CsvFile, h=T, sep=',', colClasses=c('character', 'numeric'))
N <- nrow(d)
CrossIdx = which.min(abs(d$RelScore))
d <- transform(d,
FeatureNo = 1:N,
TextOffset = (ifelse(d$RelScore > 0, -2, +2)),
TextJust = d$RelScore > 0,
FillColor = (ifelse(d$RelScore > 0, MyRed, MyGreen)),
FeatureLabels = sprintf("%s (%.1f%%)", d$FeatureName, d$RelScore)
)
g <- ggplot(
data=d,
aes(
x=FeatureNo,
y=RelScore
),
xlim(-100, 100)
) +
geom_bar(
stat='identity',
position='identity',
width=0.8,
fill=d$FillColor,
) +
geom_text(label=d$FeatureLabels,
y=d$TextOffset, x=d$FeatureNo,
size=2.0, angle=0, hjust=d$TextJust) +
ggtitle(Title) +
ylab('Relative Importance (%pct)') +
xlab(NULL) +
annotate("text", x=CrossIdx+20, y=+35, label='Weight\nGain',
angle=0, colour=MyRed, size=9,
family=Family, fontface=Face) +
annotate("text", x=CrossIdx-20, y=-35, label='Weight\nLoss',
angle=0, colour=MyGreen, size=9,
family=Family, fontface=Face) +
coord_flip() +
theme(
plot.title=title.theme,
axis.title.y=y.title.theme,
axis.title.x=x.axis.theme,
axis.text.x=x.axis.theme,
axis.text.y=element_blank()
)
ggsave(g, file=PngFile, width=W, height=H, dpi=DPI)