/
options.r
173 lines (162 loc) · 4.7 KB
/
options.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Set ggplot options
# Set global options for ggplot.
#
# These are aliased into every plot object, so that \\code{p$grid.col} will
# return the default grid colour, unless it has been overriden for a particular
# plot object. You can change the global options using the function, or the
# options for a specific plot by setting the values directly on the object. See
# the examples for more details.
#
# Colour settings:
#
# \itemize{
# \item axis.colour: axis text and line colour ("black")
# \item background.colour: background text colour ("black"), used for title
# \item background.fill: background fill ("white")
# \item grid.colour: plot grid colour ("white")
# \item grid.fill: plot grid background fill ("grey90")
# }
#
# Strip settings
#
# \itemize{
# \item strip.text: function with two arguments (variable, and value) used for
# generating strip labels
# \item strip.gp: graphic parameter settings for the strip
# \item strip.text.gp: graphic parameter settings for the strip text
# }
#
# Legend settings
#
# \itemize{
# \item legend.position: position of legend: "none" to hide legend;
# "left", "right", "top", "bottom", for positioning outside of plot;
# c(x, y) for positioning on top of plot
# \item legend.justifcation: part of legend that position refers to
# }
#
# Other settings:
#
# \itemize{
# \item aspect.ratio: aspect ratio of facets. Set to \\code{NULL} to allow
# to vary with device size
# }
#
# @arguments list of options to get/set
# @returns previous settings
# @keyword manip
# @alias ggopt
# @alias theme_default
# @alias theme_bw
#X ggopt(background.fill = "black", background.color ="white") # all new plots will use this
#X p <- qplot(total_bill, tip, facet = smoker ~ sex, data=tips)
#X p
#X p$background.fill = "white"
#X p
#X p$strip.text.gp <- gpar(col="red", fontsize=15)
#X p$strip.gp <- gpar(fill="black")
#X p$background.fill <- "black"
#X p$background.colour <- "white"
#X p$grid.colour <- "white"
#X p$grid.fill <- "grey50"
#X p # a very ugly plot!
#X ggopt(background.fill = "white", background.color ="black")
#X
#X p <- qplot(wt, mpg, data=mtcars, colour=factor(cyl))
#X p + opts(legend.position = c(0.9,0.9))
#X (p <- p + opts(legend.position = c(0.5,0.5)))
#X p + opts(legend.justification = "centre")
#X
#X DF <- data.frame(
#X x=rnorm(20),
#X y=rnorm(20),
#X g1=rep(letters[1:2], 10),
#X g2=rep(LETTERS[1:2], each=10)
#X )
#X
#X (p <- qplot(x, y, data=DF, facets = g1 ~ g2))
#X
#X p$strip.text <- function (variable, value) {
#X greek <- c("A" = "alpha", "B" = "beta")[value]
#X makelabel <- function (g) substitute(variable == greek, list(variable=as.name(variable), greek=as.name(g)))
#X
#X lapply(greek, makelabel)
#X }
#X
#X p
.build_options <- function(opt) {
class(opt) <- "options"
function(...) {
if (length(list(...)) == 0) return(opt)
old <- opt
opt <<- updatelist(opt, list(...))
invisible(old)
}
}
# Print options
#
# @keyword internal
print.options <- function(x, ...) str(x)
# Set ggplot theme.
# A theme is a list of options for \code{\link{ggopt}}.
#
# Use \code{ggtheme(defaulttheme)} to reset back to the
# default theme.
#
# @arguments theme, a list of options for \code{\link{ggopt}}
# @keyword manip
ggtheme <- function(theme) {
do.call("ggopt", theme)
}
theme_default <- structure(list(
aspect.ratio = NULL,
auto.print = FALSE,
axis.colour = "grey50",
background.colour = "black",
background.fill = "white",
border.colour = "white",
grid.colour = "white",
grid.minor.colour = "grey95",
grid.fill = "grey90",
legend.position = "right",
legend.justification = c("right", "top"),
save = FALSE,
strip.gp = gpar(col = "white", fill = "grey80", lwd=2),
strip.text = function(variable, value) value, #paste(variable, value, sep=": "),
strip.text.gp = gpar()
), class="options")
ggopt <- .build_options(theme_default)
opts <- function(...) {structure(list(...), class="options")}
theme_bw <- structure(list(
grid.colour = "grey80",
grid.minor.colour = "NA",
grid.fill = "white",
axis.colour = "black",
border.colour = "black"
), class="options")
# Access ggplot options
# Alias default options to plot object
#
# @keyword internal
"$.ggplot" <- function(x, i) {
if (i %in% names(x)) {
x[[i]]
} else {
ggopt()[[i]]
}
}
update.ggplot <- function(object, ...) {
dots <- list(...)
if (length(dots) == 1 && is.list(dots[[1]])) dots <- dots[[1]]
structure(defaults(dots, object), class="ggplot")
}
.plot_store <- function() {
.last_plot <- NULL
list(
get = function() .last_plot,
set = function(value) .last_plot <<- value
)
}
.store <- .plot_store()
set_last_plot <- function(value) .store$set(value)
last_plot <- function() .store$get()