Path: blob/master/sandbox/RFinance2014/libraries/widgets/nvd3/examples.R
1433 views
## {title: Scatter Chart}1p1 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')2p1$xAxis(axisLabel = 'Weight')3p1456## {title: MultiBar Chart}7hair_eye = as.data.frame(HairEyeColor)8p2 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')9p2$chart(color = c('brown', 'blue', '#594c26', 'green'))10p21112## {title: MultiBar Horizontal Chart}13p3 <- nPlot(~ cyl, group = 'gear', data = mtcars, type = 'multiBarHorizontalChart')14p3$chart(showControls = F)15p31617## {title: Pie Chart}18p4 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')19p42021## {title: Donut Chart}22p5 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')23p5$chart(donut = TRUE)24p52526## {title: Line Chart}27data(economics, package = 'ggplot2')28p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')29p63031## {title: Line with Focus Chart }32ecm <- reshape2::melt(economics[,c('date', 'uempmed', 'psavert')], id = 'date')33p7 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineWithFocusChart')34#test format dates on the xAxis35#also good test of javascript functions as parameters36#dates from R to JSON will come over as number of days since 1970-01-0137#so convert to milliseconds 86400000 in a day and then format with d338#on lineWithFocusChart type xAxis will also set x2Axis unless it is specified39p7$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )40#test xAxis also sets x2Axis41p742#now test setting x2Axis to something different43#test format dates on the x2Axis44#test to show %Y format which is different than xAxis45p7$x2Axis( tickFormat="#!function(d) {return d3.time.format('%Y')(new Date( d * 86400000 ));}!#" )46p747#test set xAxis again to make sure it does not override set x2Axis48p7$xAxis( NULL, replace = T)49p75051## {title: Stacked Area Chart}52dat <- data.frame(t=rep(0:23,each=4),var=rep(LETTERS[1:4],4),val=round(runif(4*24,0,50)))53p8 <- nPlot(val ~ t, group = 'var', data = dat, type = 'stackedAreaChart', id = 'chart')54p8555657## {title: InteractiveGuidline(Multi-Tooltips) on Line}58p9 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineChart')59p9$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )60#try new interactive guidelines feature61p9$chart(useInteractiveGuideline=TRUE)62p9636465## {title: InteractiveGuidline(Multi-Tooltips) on Stack}66p10 <- p867p10$chart(useInteractiveGuideline=TRUE)68p106970## {title: showDistX and showDistY}71p11 <- p172p11$chart(showDistX = TRUE, showDistY = TRUE)73p117475## {title: multiChart}76p12 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'multiChart')77p12$params$multi = list(78uempmed = list(type="area",yAxis=1),79psavert = list(type="line",yAxis=2)80)81p12$setTemplate(script = system.file(82"/libraries/nvd3/layouts/multiChart.html",83package = "rCharts"84))85p128687## {title: Facets}88p13 <- nPlot(mpg ~ wt, data = mtcars, group = "gear", type = "scatterChart")89p13$params$facet = "cyl"90p13$templates$script = system.file(91"/libraries/nvd3/layouts/nvd3FacetPlot.html",92package = "rCharts"93)94p139596hair_eye = as.data.frame(HairEyeColor)97p14 <- nPlot(Freq ~ Hair, group = 'Sex', data = hair_eye, type = 'multiBarChart')98p14$params$facet="Eye"99p14$templates$script = system.file(100"/libraries/nvd3/layouts/nvd3FacetPlot.html",101package = "rCharts"102)103p14104105p15 <- nPlot(Freq ~ Hair, group = 'Eye', data = hair_eye, type = 'multiBarChart')106p15$params$facet="Sex"107p15$templates$script = system.file(108"/libraries/nvd3/layouts/nvd3FacetPlot.html",109package = "rCharts"110)111p15112113114## {title: Sparklines}115p16 <- nPlot(uempmed ~ date, data = economics, type = 'sparklinePlus',height=100,width=500)116p16$chart(xTickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#")117p16118## semi replicate sparkline with a full nvd3 model by setting short height and turning off lots of things119p17 <- nPlot(120x = "date",121y = "volume",122data = spy.df,123type = "multiBarChart",124height = 200)125p17$chart(showControls = FALSE, showLegend = FALSE, showXAxis = FALSE, showYAxis = FALSE)126p17$xAxis(tickFormat =127"#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#"128)129p17130131132## {title: ohlcBar}133## ohlcBar not fully implemented on nvd3 side, so no axes or interactive controls134## note do not melt if using ohlcBar135require(quantmod)136137spy <- getSymbols("SPY",auto.assign=FALSE,from="2013-01-01")138colnames(spy) <- c("open","high","low","close","volume","adjusted")139140spy.df <- data.frame(index(spy),spy)141colnames(spy.df)[1] <- "date"142143p18 <- nPlot(144x = "date",145y = "close",146data = spy.df,147type = "ohlcBar"148)149p18150151152