Reputation: 3285
Highcharts 3D with rCharts
Can rCharts be made to work with the 3D module of Highcharts? Below is an example of Highcharts 3D:
The error that appears for my example code below is:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘zAxis’ is not a valid field or method name for reference class “Highcharts”
Here is the example code I'm using:
library(rCharts)
# Based on example from: http://jsfiddle.net/highcharts/V37Vv/light/
plotData <- data.frame(x = c(1,1,1,2,2,4,4,7,7,8),
y = c(1,1,1,3,6,5,2,1,1,1),
z = c(1,2,5,2,4,7,8,3,5,5),
category=rep(c("A", "B"), 5), stringsAsFactors=FALSE)
xLabel <- "X"
yLabel <- "Y"
zLabel <- "Z"
title <- "Title"
if (is.null(rownames(plotData))){
rownames(plotData) <- as.character(1:nrow(plotData))
}
# HighCharts point name
plotData$name <- rownames(plotData)
# Scatter plot
h1 <- rCharts::Highcharts$new()
h1$setLib("libraries/widgets/highcharts")
h1$chart(renderTo="container",
margin=c(150, 75, 75, 75),
type='scatter',
options3d=list(enabled=TRUE,
alpha=20,
beta=30,
depth=200))
h1$title(text=title)
h1$xAxis(title=list(enabled=TRUE, text=xLabel))
h1$yAxis(title=list(enabled=TRUE, text=yLabel))
h1$zAxis(title=list(enabled=TRUE, text=zLabel))
# Divide the dataset, split by category and put into list() format
# From: http://rcharts.io/viewer/?5735146#.VF6NS4W1Fy4
series <- lapply(split(plotData, plotData$category), function(x) {
res <- lapply(split(x, rownames(x)), as.list)
names(res) <- NULL
return(res)
})
invisible(sapply(series, function(x) {
h1$series(data=x, type="scatter", name=x[[1]]$category)
}
))
h1$legend(enabled=FALSE)
# Force circle markers and change size
h1$plotOptions(scatter=list(marker=list(symbol='circle', radius=6)))
h1$tooltip(formatter = "#! function() { return this.point.name + ', ' +
this.series.name + ', ' + this.x + ', ' + this.y; } !#")
# Enable exporting
h1$exporting(enabled=TRUE)
# Set name
h1$set(dom=xLabel)
# Print chart
print(h1)
#h1$show("inline")
This folder contains a config.yml:
highcharts:
jshead:
- js/jquery-1.7.min.js
- js/highcharts.js
- js/highcharts-more.js
- js/exporting.js
- js/highcharts-3d.js
cdn:
jshead:
- "http://code.jquery.com/jquery-1.7.min.js"
- "http://code.highcharts.com/highcharts.js"
- "http://code.highcharts.com/highcharts-more.js"
- "http://code.highcharts.com/highcharts-3d.js"
- "http://code.highcharts.com/modules/exporting.js"
The js/ folder has the files referenced in the config.yml
The layouts/ folder has chart.html
<script type='text/javascript'>
(function($){
$(function () {
var chart = new Highcharts.Chart({{{ chartParams }}});
});
})(jQuery);
</script>
Upvotes: 2
Views: 1167
Reputation: 3029
Now there is another highcharts wrapper for R. Include the 3d module. Example here: http://jkunst.com/highcharter/highcharts-api.html.
Code
data(citytemp)
hc <- highchart() %>%
hc_xAxis(categories = citytemp$month) %>%
hc_add_series(name = "Tokyo", data = citytemp$tokyo) %>%
hc_add_series(name = "London", data = citytemp$london) %>%
hc_add_series(name = "Other city",
data = (citytemp$tokyo + citytemp$london)/2) %>%
hc_chart(type = "column",
options3d = list(enabled = TRUE, beta = 15, alpha = 15))
hc
Upvotes: 4
Reputation: 2261
I do not think 3D charts are available in rCharts, hence the error on the nonexistent z axis.
Instead you have to use the package rthreejs based on Ramnathv's htmlwidget package which also works in Rmarkdown and Shiny.
devtools::install_github("ramnathv/htmlwidgets")
devtools::install_github("bwlewis/rthreejs")
libs <- c("htmlwidgets", "threejs")
lapply(libs, library, character.only=TRUE)
Example on htmlwidget galery
Upvotes: 0