Interactive visualizations
using R
Thomas Reinholdsson
a <- rPlot(mpg ~ wt | am + vs, data = mtcars, type = 'point', color = 'gear')
a
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
a <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
a$set(pointSize = 0, lineWidth = 1)
a
hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
a <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male,
type = 'multiBarChart')
a
require(reshape2)
uspexp <- melt(USPersonalExpenditure)
names(uspexp)[1:2] = c('category', 'year')
a <- xPlot(value ~ year, group = 'category', data = uspexp,
type = 'line-dotted')
a
usp = reshape2::melt(USPersonalExpenditure)
a <- Rickshaw$new()
a$layer(value ~ Var2, group = 'Var1', data = usp, type = 'area')
a$set(width = 600, height = 350)
a
library(rCharts)
hPlot(Pulse ~ Height, data = MASS::survey, type = "scatter", group = "Exer")
library(rCharts)
a <- Highcharts$new()
a$chart(type = "line")
a$title(text = "Monthly Average Rainfall")
...
highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
...
Why JavaScript?
Package structure
<script type='text/javascript'>
(function($){
$(function () {
var chart = new Highcharts.Chart();
});
})(jQuery);
</script>
highcharts:
jshead:
- js/jquery-1.9.1.min.js
- js/highcharts.js
- js/highcharts-more.js
- js/exporting.js
cdn:
jshead:
- "http://code.jquery.com/jquery-1.9.1.min.js"
- "http://code.highcharts.com/highcharts.js"
- "http://code.highcharts.com/highcharts-more.js"
- "http://code.highcharts.com/modules/exporting.js"
# Class defintion
Highcharts <- setRefClass("Highcharts", contains = "rCharts", methods = list(
initialize = function() {
callSuper(); lib <<- 'highcharts'; LIB <<- get_lib(lib)
params <<- c(params, list(
...
)
)
},
...
# Wrapper methods
title = function(..., replace = T) {
params$title <<- setSpec(params$title, ..., replace = replace)
},
tooltip = function(..., replace = T) {
params$tooltip <<- setSpec(params$tooltip, ..., replace = replace)
},
xAxis = function(..., replace = T) {
params$xAxis <<- setListSpec(params$xAxis, ..., replace = replace)
},
...
a <- Highcharts$new()
a$chart(type = "spline", backgroundColor = NULL)
a$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
a$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 1), dashStyle = "shortdot")
a$legend(symbolWidth = 80)
a$set(height = 250)
a
a$chart(type = "bar", backgroundColor = NULL)
a$xAxis(title = list(enabled = F), type = "category", lineWidth = 0, minorTickLength = 0, tickLength = 0)
a$yAxis(title = list(enabled = F), labels = list(enabled = F), lineWidth = 0, gridLineWidth = 0, minorTickLength = 0, tickLength = 0)
a$legend(enabled = F)
a$tooltip(
useHTML = T,
formatter = "#! function() {
return '<table height=84><tr><td>'
+ '<img src=\"'
+ this.point.url
+ '\" height=80 width=60></td><td>'
+ this.point.text + '<br><br>' + this.point.x + ' years<br>' + this.point.valkrets
+ '</td></tr></table>';} !#"
)
a$plotOptions(
scatter = list(
cursor = "pointer",
point = list(
events = list(
click = "#! function() { window.open(this.options.url); } !#")),
marker = list(
symbol = "circle",
radius = 5
)
)
)
a <- create_chart("code.r")
a$publish("My Chart", host = "gist")
# Please enter your github username: reinholdsson
# Please enter your github password: ************
# Your gist has been published
# View chart at http://rcharts.github.io/viewer/?11519927
library(shiny)
library(rCharts)
shinyServer(function(input, output) {
output$text <- renderText({
sprintf("You have clicked on %s.", input$click$capital)
})
output$chart <- renderChart({
a <- Highcharts$new()
a$series(data = list(
list(x = 0, y = 40, capital = "Stockholm"),
list(x = 1, y = 50, capital = "Copenhagen"),
list(x = 2, y = 60, capital = "Oslo")
), type = "bar")
a$plotOptions(
bar = list(
cursor = "pointer",
point = list(
events = list(
click = "#! function() { Shiny.onInputChange('click', {capital: this.capital})} !#"
))))
a$addParams(dom = "chart")
return(a)
})
})
library(shiny)
library(rCharts)
shinyUI(bootstrapPage(chartOutput("chart", "highcharts"), textOutput("text")))
ichoropleth(
Crime ~ State,
data = subset(violent_crime, Year == 2010),
pal = 'PuRd'
)
ichoropleth(
Crime ~ State,
data = violent_crime,
animate = 'Year'
)
play = T
to add a play button.# data <- ...
a <- rMaps:::Leaflet$new()
a$setView(c(59.34201, 18.09503), zoom = 13)
a$geoJson(data,
onEachFeature = '#! function(feature, layer){
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);
}
} !#',
pointToLayer = "#! function(feature, latlng){
return L.circleMarker(latlng, {
radius: 6,
fillColor: feature.properties.fillColor,
weight: 1,
fillOpacity: 0.8
})
}!#"
)
a$set(height = 300)
a
dat <- data.frame(From=c(rep("A",3), rep("B", 3)),
To=c(rep(c("X", "Y", "Z"),2)),
Weight=c(5,7,6,2,9,4))
a <- gvisSankey(dat, from="From", to="To", weight="Weight",
options=list(height=250))
a <- gvisCalendar(Cairo, datevar="Date",
numvar="Temp",
options=list(calendar="{ cellSize: 10 }"))
Use a spacebar or arrow keys to navigate