-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.R
executable file
·74 lines (59 loc) · 1.73 KB
/
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
#!/usr/bin/Rscript
library(yaml)
strip_yaml_extension = function (filename) {
return(strsplit(filename, "\\.yaml")[[1]][1]);
}
# parse the command line arguments
args = commandArgs(T)
if (length(args) < 1 || length(args) > 3) {
print("usage: ", quote=F)
print(" plot.R <yaml file> [<data file> [<plot file>]]", quote=F)
quit(status=1)
}
yamlfilename = args[1]
if (length(args) >= 2) {
datafilename = args[2]
} else {
datafilename = paste0(strip_yaml_extension(yamlfilename), ".csv")
}
if (length(args) >= 3) {
plotfilename = args[3]
} else {
plotfilename = paste0(strip_yaml_extension(yamlfilename), ".pdf")
}
# read in the yaml file
y = yaml.load_file(yamlfilename)
# read in the csv file
d = read.csv(datafilename)
# choose appropriate x and y columns
x_cols = y$plot_x_variable
if (is.null(x_cols)) {
x_cols = colnames(d)[1]
}
y_cols = y$plot_y_variable
if (is.null(y_cols)) {
y_cols = colnames(d)[2:(dim(d)[2])]
}
# use the smallest and largest values in the first column as the x-range
xmin = min(d[,x_cols], na.rm=T)
xmax = max(d[,x_cols], na.rm=T)
# use the remaining columns for the y-rang
ymin = min(d[,y_cols], na.rm=T)
ymax = max(d[,y_cols], na.rm=T)
# use points iff there are 50 or fewer values, otherwise use lines
if (length(d[,x_cols]) <= 50) {
plottype = "p"
} else {
plottype = "l"
}
# create a new plot
pdf(plotfilename)
plot(d[,x_cols], d[y_cols][,1], xlim=c(xmin,xmax), ylim=c(ymin,ymax), type=plottype,
main=y$plot_title, xlab=y$plot_x_axis_label, ylab=y$plot_y_axis_label)
if (length(y_cols) > 1) {
colors = rainbow(floor(length(y_cols) * 1.5))
for (i in 2:length(y_cols)) {
lines(d[,x_cols], d[y_cols][,i], type=plottype, col=colors[i-1])
}
}
dev.off()