Skip to content

Commit

Permalink
1. use mids not breaks in echart_hist()
Browse files Browse the repository at this point in the history
2. simple functions for add line and points to plot. Not tested yet.
3. start to wrap an eSeries function to add Series attributes.
  • Loading branch information
Liyun Chen committed May 1, 2015
1 parent 6452641 commit 7135289
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
61 changes: 61 additions & 0 deletions R/add_line.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
echart_stat_line = function ( stat, name = NULL){
# stat is the statisitic of such hline,
check_stat(stat)
if (length(stat) > 1) stop("Only one stat each time.")
list(markLine= list(data = list(type = stat, name = name )))
}

# a more general function to add any line
echart_abline_point = function ( start, end, name = NULL){
# start is a vector with two values (x,y)
# end is a vector with two values (x,y)
if (!is.numeric(start) | !is.numeric(end)) stop("start and end should be numeric vectors.")
if (length(start)!=2 | length(end) != 2) stop("start and end should be a vector of two numeric values.")
if (Inf %in% c(start, end)) {
warning("Cannot draw infinitie line.") # check if echart can handle Inf; if not, replace Inf

}
list(markLine =
list(data =
c(list( name = 'line start', value = 1, x = start[1], y = start[2]),
list( name = 'line end', x = end[1], y = end[2]))
))

}

# wrap of abline(); add line by intercept and slope
echart_abline = function ( intercept, slope, name = NULL,
# put some defaults here, could be extended
xmin = -100,
xmax = 100,
ymin = -100,
ymax = 100){
# start is a vector with two values (x,y)
# end is a vector with two values (x,y)
y_min_actual = xmin * slope + intercept
y_max_actual = xmax * slope + intercept

start = c(xmin, y_min_actual)
end = c(xmax, y_max_actual)

# ignore ymin and ymax for now
echart_abline_point(start, end, name = name)
}

# wrap function for horizontal line
echart_hline = function (yintercept, name = NULL){
if (!is.numeric(y)) stop("yintercept must be numeric.")
if (length(yintercept)>1) stop("only one line each time.")
echart_abline(intercept = yintercept, slope = 0)
}

# wrap function for vertical line

echart_vline = function (xintercept, name = NULL){
if (!is.numeric(y)) stop("yintercept must be numeric.")
if (length(xintercept)>1) stop ("one line each time.")
start = c(xintercept, -Inf)
end = c(xintercept, Inf)
echart_abline_point(start, end, name = name)
}

18 changes: 18 additions & 0 deletions R/add_point.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# function to add points on plot
echart_point = function (x, y, name = NULL, value = NULL){
if (!is.numeric(x) | !is.numeric(y)) { stop("x and y should be numeric.")}
list(markPoint = list(data = list(name = name, value = value, x = x, y = y)))
}

# add statistic point directly
echart_stat_point = function (stat, name = NULL, value = NULL){
check_stat(stat)
list(markPoint = list(data = list(name = name, type = stat)))
}

check_stat = function (stat){
if (!stat %in% c("min","max","average")){
stop("stat should be either min, max or avergae.")
}
}

4 changes: 0 additions & 4 deletions R/echart.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ echart.data.frame = function(
params$legend = list(data = levels(as.factor(series)))
}

if("barCategoryGap" %in% names(list(...))){
for (i in 1:length(params$series))
{params$series[[i]]$barCategoryGap = list(...)$barCategoryGap}
}

chart = htmlwidgets::createWidget(
'echarts', params, width = width, height = height, package = 'recharts',
Expand Down
4 changes: 3 additions & 1 deletion R/echart_hist.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ echart_hist = function(data, binwidth = NULL){
} else{
bar_hist = hist(data, plot = FALSE , binwidth = binwidth)
}
# adjust the breaks
# bar_hist$x = round(bar_hist$mids,2)

echart(bar_hist, ~breaks, ~counts, type ="bar", barCategoryGap='0%')
echart(bar_hist, ~mids, ~counts, type ="bar", barCategoryGap='0%')
}
6 changes: 6 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ axisType = function(data, which = c('x', 'y')) {
str(data)
stop('Unable to derive the axis type automatically from the ', which, ' variable')
}

eSeries = function (chart,barCategoryGap, ...){
# basically we need to add barCategoryGap to series
params$series[[i]]$barCategoryGap = list(...)$barCategoryGap

}

0 comments on commit 7135289

Please sign in to comment.