-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy patheRadar.R
More file actions
193 lines (173 loc) · 9.11 KB
/
eRadar.R
File metadata and controls
193 lines (173 loc) · 9.11 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#' Create an HTML radar charts widget using the ECharts(version:3.2.2) library
#'
#' This function creates an HTML widget to display data.frame using the JavaScript library ECharts3.
#' @param dat a data object (a data frame or a factor array)
#' @param xvar,yvar objects of class "formula" (or one that can be coerced
#' to that class): x,y coordinates of the given data.frame colnames, e.g.
#' \code{xvar = ~xAxisName}; \code{yvar = ~yAxisName}. xvar, yvar are needed for the
#' data.frame data input.
#' @param series an "formula" object: Associates the levels of variable
#' with symbol color, e.g. \code{series = ~groupName}
#' @param size an array of html widget width and height(either numeric pixels
#' or percentage could be accepted): e.g. size = c(1024, 768).
#' @param ymin,ymax a numeric array with the same length of unique(xvar), to set the
#' limitation for each polar axis limitation. if you want to set someone to be default
#' limitation, you can set the given array index to NULL, examples input:
#' \code{ymin = c(NULL, 1000, 2000, 3000)}
#' @param theme an object of theme name. see(\url{http://datatables.net/extensions/index}) for detail.
#' supported theme: \code{c("default", "vintage", "dark", "westeros", "essos", "wonderland", "walden",
#' "chalk", "infographic", "macarons", "roma", "shine", "purple-passion")}
#' @param title an overall title for the plot. you can modify title widget after chart has been
#' generated: Echart + eTitle(title = "your title.")
#' @param title.x,title.y the xy coordinates of main title, besides the excat exact pixels value,
#' x accept c("left", "right", "center") and y accept c("top", "bottom", "center") as legal input.
#' you can modify title widget after chart has been generated:
#' \code{Echart + eTitle(title="main title", x = "left", y=10)}
#' @param legend logical whether the legend widget show or not, default is TRUE.
#' you can modify legend widget after chart has been generated, the legend position and
#' legend orientation are available at present.
#' \code{Echart + eLegend(show = TRUE)}
#' @param legend.x,legend.y the xy coordinates of legend, besides the excat exact pixels value,
#' x accept c("left", "right", "center") and y accept c("top", "bottom", "center") as legal input.
#' you can modify legend widget after chart has been generated:
#' \code{Echart + eLegend(x = "right", y="top")}
#' @param legend.orient an element of c("horizontal", "vertical"), default is "horizontal"
#' you can modify legend widget after chart has been generated:
#' \code{Echart + eLegend(orient = "vertical")}
#' @param toolbox logical whether the toolbox widget show or not, default is TRUE.
#' you can modify toolbox widget after chart has been generated, the toolbox position, toolbox
#' element and toolbox orientation are available at present.
#' \code{Echart + eToolbox(show = TRUE)}
#' @param toolbox.x,toolbox.y the xy coordinates of toolbox, besides the excat exact pixels value,
#' x accept c("left", "right", "center") and y accept c("top", "bottom", "center") as legal input.
#' you can modify toolbox widget after chart has been generated:
#' \code{Echart + eToolbox(x = "right", y="top")}
#' @param toolbox.orient an element of c("horizontal", "vertical"), default is "horizontal"
#' you can modify toolbox widget after chart has been generated:
#' \code{Echart + eToolbox(orient = "vertical")}
#' @param dataview,mark,restore,dataZoom,magicType logical variable whether the dataview
#' , mark, restore, dataZoom or magicType tool in toolbox widget show or not,
#' default is TRUE. you can modify toolbox widget after chart has been generated,
#' the toolbox position, toolbox element and toolbox orientation are available at present.
#' \code{Echart + eToolbox(dataView = FALSE)}
#' @param tooltip logical whether the tooltip widget for front-end interactive chart
#' show or not. default is TRUE. you can modify tooltip widget after chart has been generated,
#' the tooltip trigger and tooltip formatter is available at present.
#' \code{Echart + eTooltip(show = FALSE)}
#' @param tooltip.trigger an element of c("axis", "item"), default is "axis" for radar chart.
#' "axis" option for trigger will show all the information of mouse;
#' "item" option for tirgger will only show the given item information of mouse.
#' you can modify tooltip widget after chart has been generated:
#' \code{Echart + eTooltip(trigger = "axis")}
#' @param tooltip.formatter the information formatter for tooltip widget,
#' default is "<a>:<b><c>" for radar chart.
#' you can modify tooltip widget after chart has been generated:
#' \code{Echart + eTooltip(formatter = "<a><b>:<c>")}
#' @param calculable logical whether the front-end interactive chart will
#' support the drag-recalculable feature.
#' the size and calculable option can be setted after radar chart has been
#' generated through eOption: \code{Echart + eOption(calculable = TRUE)}
#' @param showLabel logical whether the region name label showed on chart.
#' default is TRUE, e.g. \code{Echart + eOption(showLabel = TRUE)}
#' @note You are recommended to use lazyPlot function for interactive chart
#' option set through "shiny" server.
#' @export
#' @examples
#' require(plyr)
#' dat = ddply(iris, .(Species), colwise(mean))
#' rownames(dat) = dat[,1]
#' dat = dat[, -1]
#' dat
#' eRadar(dat)
#' df2 <- data.frame(
#' saleNum=c(10,20,30,40,50,60,70,15,25,35,45,55,65,75,25,35,45,55,65,75,85),
#' seller=c(rep("Yellow",7), rep("Red",7), rep("White",7)),
#' weekDay = c(rep(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"),3))
#' )
#' dat <- df2
#' xvar=~weekDay; yvar= ~saleNum; series=~seller
#' eRadar(df2, ~weekDay, ~saleNum, ~seller)
eRadar = function(dat, xvar=NULL, yvar=NULL, series=NULL, size = NULL, ymin=vector(), ymax=vector(),
theme = "default", title = NULL, subtitle = NULL, title.x = "center", title.y = "top",
legend = TRUE, legend.data=NULL, legend.x = "left", legend.y= "top", legend.orient="horizontal",
toolbox = TRUE, toolbox.orient = "horizontal", toolbox.x = "right", toolbox.y = "top",
dataView = TRUE, readOnly = FALSE, mark=TRUE, dataZoom=FALSE, magicType=FALSE,
tooltip = TRUE, tooltip.trigger="item", formatter="", axis.scale=TRUE,
xlab=FALSE, ylab=FALSE, calculable=TRUE, showLabel=TRUE, opt = list())
{
xlabName = autoArgLabel(xvar, deparse(substitute(xvar)))
ylabName = autoArgLabel(yvar, deparse(substitute(yvar)))
xvar = as.factor(evalFormula(xvar, dat))
yvar = evalFormula(yvar, dat)
series = as.factor(evalFormula(series, dat))
# data flow format..
# if series is null, we will use the xvar and yvar to construct the bar plot..
if(is.null(xvar) & is.null(yvar) & !is.factor(dat)){
# Mode 1. use default data.frame as input...
dat <- as.data.frame(dat, stringsAsFactor=F)
}else if(!is.null(xvar) & !is.null(yvar) & !is.null(series)){
#print("Mode1")
# Mode 2. all of xvar, yvar and series are valid...
dat <- with(dat, {
out <- matrix(nrow=nlevels(series), ncol=nlevels(xvar),
dimnames=list(levels(series), levels(xvar)))
out[cbind(series, xvar)] <- yvar
out
})
dat <- as.data.frame(dat)
}else if(!is.null(xvar) & !is.null(yvar) & is.null(series)){
# Mode 3. format dat with only x and y variable.
dat <- data.frame(val = yvar)
colnames(dat) <- ylabName
rownames(dat) <- xvar
}else if(is.null(xvar) & is.null(yvar) & is.factor(dat)){
# Mode 4. factor
tempD <- as.data.frame(table(dat))
dat <- data.frame(val = tempD[,"Freq"])
colnames(dat) <- "Frequency"
rownames(dat) <- tempD[,1]
}
# option$title format.
opt$title = tilteSet(title = title, subtitle=subtitle,
title.x = title.x, title.y = title.y)
opt$calculable = calculableSet(calculable = calculable)
opt$theme = themeSet(theme = theme)
# opt$tooltip format, not open to user now.
opt$tooltip = tooltipSet( tooltip=tooltip,trigger=tooltip.trigger,
formatter = "", islandFormatter="")
opt$toolbox = toolboxSet(toolbox=toolbox, toolbox.x=toolbox.x, toolbox.y=toolbox.y, orient=toolbox.orient,
dataView=dataView, mark=mark, dataZoom = dataZoom, magicType = magicType, restore = TRUE, readOnly = readOnly,
saveAsImage=TRUE)
if(missing(legend.data) | is.null(legend.data)){legendData = rownames(dat)
}else{legendData = legend.data}
opt$legend = legendSet( show=legend, data=legendData, legend.x=legend.x, legend.y=legend.y, orient=legend.orient)
opt$polar = list(polarSet(name=colnames(dat), ymin=ymin, ymax=ymax))
datList = vector("list", nrow(dat))
for(i in 1:nrow(dat)){
datList[[i]]$name = rownames(dat)[i]
datList[[i]]$value = unnames(unlist(dat[i,]))
}
names(datList) = NULL
if(is.null(opt$series)) {
opt$series = vector("list", 1)
}
if(is.null(opt$series[[1]]$type)) {
opt$series[[1]]$type = 'radar'
}
if(is.null(opt$series[[1]]$data)) {
opt$series[[1]]$data = datList
}
opt$size = size
### output list format
chart = htmlwidgets::createWidget(
'echarts', opt,
package = 'recharts', width = size[1], height = size[2],
preRenderHook = function(instance) {
instance
}
)
chart = .addClass(chart, "eRadar")
# add theme dependencies
chart = addThemeDependencies(chart)
chart
}