-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgsplot-class.R
More file actions
135 lines (115 loc) · 4.23 KB
/
gsplot-class.R
File metadata and controls
135 lines (115 loc) · 4.23 KB
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
#' gsplot
#'
#' Used to change the class of inputs to "gsplot".
#'
#' @param x list
#' @param created vector of length one giving the date the gsplot object was created. Defaults to
#' using \code{Sys.Date()}. Output class matches that of the input.
#' @param gsplot.version vector of length one giving the version of the gsplot package used to create the
#' object. Defaults to calling \code{packageDescription()}. Output class matches that of the input.
#' @param config.file path to the file that will only be used for setting
#' par in this one gsplot object. If \code{NA} (default), par is set by the global options set by
#' loadConfig().
#' @param theme There are several built in themes (see \link{Themes}). Additionally, the user can create a \code{gsplot}
#' object in their workspace. This argument then takes the name of that object (either built-in or custom).
#' If \code{NA} (default), no theme is used.
#' @param frame.plot a logical indicating whether a box should be drawn around the plot. Default is \code{TRUE}.
#' @param \dots Further graphical parameters may also be supplied as arguments. See 'Details'.
#' @return gsplot
#' @export
#' @rdname gsplot
#' @importFrom methods getPackageName
#' @importFrom utils packageDescription
#' @examples
#' gsplot()
#' gsplot(theme = theme.hadley)
#' gs_config <- gsplot(config.file =
#' system.file("extdata", "lineScatter.yaml", package = "gsplot")) %>%
#' lines(1:10, 1:10)
#'
#' gs_config
#'
#' gs <- gsplot(theme = theme.hadley) %>%
#' points(1:10, 1:10, xlab="Index")
#' gs
#'
gsplot <- function(x = NULL, ...) UseMethod("gsplot")
#' @rdname gsplot
#' @export
gsplot.default <- function(..., created=Sys.Date(),
gsplot.version=packageDescription(getPackageName(),
fields = "Version"),
config.file=NA, theme=NA, frame.plot=TRUE) {
user.config <- config.file
if(!all(is.na(theme))){
object <- theme
if("metadata" %in% names(object)){
object <- object[-which(names(object) == "metadata")]
}
if(is.na(config.file) &&
"config.file" %in% names(theme[["global"]][["config"]])){ #if no config file specified by user
config.file <- theme$global$config$config.path
}
object[["global"]][["config"]][["config.file"]] <- !is.na(config.file)
object[["global"]][["config"]][["config.path"]] <- config.file
} else {
object <- list(global= list(config=list(frame.plot=frame.plot,
config.file = !is.na(config.file),
config.path = config.file)))
}
object <- c(list(metadata = list(created=created,
gsplot.version=gsplot.version)),
object)
if (!is.na(user.config)){
object[["config"]] <- yaml.load_file(config.file)
}
if(object[["global"]][["config"]]$config.file){
load_temp_config(object)
}
if(length(all.equal(gsconfig$original.par, par(no.readonly = TRUE))) > 1){
par(gsconfig$original.par)
}
if(!("par" %in% names(object[["global"]]))){
object <- add_new_par(object, 'global')
}
object <- gsplot(object)
if(length(list(...)) > 0){
object <- par(object, ...)
}
return(object)
}
#' @rdname gsplot
#' @exportMethod gsplot
gsplot.list <- function(x){
class(x) <- "gsplot"
invisible(x)
}
#' Summary of gsplot object
#'
#' Summary information
#'
#' @param object list
#' @param \dots additional parameters
#' @export
#' @examples
#' gs <- gsplot() %>%
#' points(1:10,1:10) %>%
#' axis(side=1, at=seq(1,10,length.out=18),las=3) %>%
#' axis(side=3, labels=FALSE) %>%
#' grid(side=c(1,2),col="green") %>%
#' grid(side=c(3,4))
#' summary(gs)
summary.gsplot <- function(object,...){
view.info <- view_info(object)
cat("Summary information of plotting object:\n")
cat(nrow(view.info),"views:\n")
for(i in seq_len(nrow(view.info))){
cat("View:",i,"\nx side:", view.info$x[i], ",y side:", view.info$y[i], "\n")
cat("xlim:",xlim(object, side=view.info$x[i]),",")
cat("ylim:",ylim(object, side=view.info$y[i]))
if(view.info$log[i] != ""){
cat(",log:",view.info$log[i])
}
cat("\n")
}
}