-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_xyplot.R
74 lines (71 loc) · 3.06 KB
/
create_xyplot.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
# Create a lattice xy plot object in memory ------------------------------------------------
# Purpose: to construct a valid lattice log_x-y plot of one or more series
# Description: The argument `the_data_frame` is in long form, where each row has data
# for a sub-group. The list of arguments to construct the plot are:
# 1. the name of the variable that defines the subgroups, called subgroup_variable
# 2. a list of text labels for title, x and y axis, called text_labels_for_plot
# 3. a vector of the min and max values to use for the y axis, min_max_y_to_plot
# 4. a vector of text descriptions for every series to plot, or NULL, called subgroup_descriptions
create_xyplot <- function(the_data_frame,
args_to_plot) {
# unpack arguments
the_subgroup_variable_name <- args_to_plot[['subgroup_variable']]
the_text_plot_labels <- args_to_plot[['text_labels_for_plot']]
min_max_y_to_plot <- args_to_plot[['min_max_y_to_plot']]
the_subgroup_descriptions <- args_to_plot[['subgroup_descriptions']]
# setup values for plotting
group_labels <- as.vector(unique(the_data_frame[the_subgroup_variable_name]))
wager_labels <- as.vector(sapply( group_labels, FUN = function(x) {paste0("Wager ", x)}))
if (is.null(the_subgroup_descriptions)) {
the_subgroup_descriptions <- wager_labels
}
wagers_vector <- the_data_frame[[the_subgroup_variable_name]]
max_x_value <- length(wagers_vector[wagers_vector==group_labels[[the_subgroup_variable_name]][1]])
xyplot(
runningprofit ~ tries,
data = the_data_frame,
groups = wager,
par.settings = list(superpose.line = list(
col = c("blue", "red", "green", "yellow", "brown", "cyan"),
lwd = 1
)),
auto.key = list(
space = "top",
columns = 2,
text = the_subgroup_descriptions,
title = the_text_plot_labels[['title']],
cex.title = 2,
lines = TRUE,
points = FALSE
),
xlab = the_text_plot_labels[['x_label']],
xlim = c(1, max_x_value),
scales = list(
cex = c(1.1, 1.1), # increase font size
x = list(log = T), # log scale for x-axis
y = list(log = F),
alternating = 1, # axes labels left/bottom
tck = c(1, 0)
), # ticks only with labels
ylab = the_text_plot_labels[['y_label']],
ylim = min_max_y_to_plot,
type = c("l"),
panel = panel.superpose,
panel.groups = function(x, y, group.number, ...) {
panel.abline(h = y[which(y == 0.0)],
lty = "dotted",
col = "black")
panel.grid(v = -1,
h = -1,
lty = 3)
xt <- x[x == log(min(x) + 1)] # find x coordinate for first point
yt <- y[x == min(x)] # find y coordinate for first point
panel.text(xt,
yt,
labels = wager_labels[group.number],
pos = 4, # show labels on right side
...)
panel.xyplot(x, y, ...)
}
)
}