Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/RFinance2014/libraries/widgets/nvd3/examples.R
1433 views
1
## {title: Scatter Chart}
2
p1 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
3
p1$xAxis(axisLabel = 'Weight')
4
p1
5
6
7
## {title: MultiBar Chart}
8
hair_eye = as.data.frame(HairEyeColor)
9
p2 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')
10
p2$chart(color = c('brown', 'blue', '#594c26', 'green'))
11
p2
12
13
## {title: MultiBar Horizontal Chart}
14
p3 <- nPlot(~ cyl, group = 'gear', data = mtcars, type = 'multiBarHorizontalChart')
15
p3$chart(showControls = F)
16
p3
17
18
## {title: Pie Chart}
19
p4 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')
20
p4
21
22
## {title: Donut Chart}
23
p5 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')
24
p5$chart(donut = TRUE)
25
p5
26
27
## {title: Line Chart}
28
data(economics, package = 'ggplot2')
29
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
30
p6
31
32
## {title: Line with Focus Chart }
33
ecm <- reshape2::melt(economics[,c('date', 'uempmed', 'psavert')], id = 'date')
34
p7 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineWithFocusChart')
35
#test format dates on the xAxis
36
#also good test of javascript functions as parameters
37
#dates from R to JSON will come over as number of days since 1970-01-01
38
#so convert to milliseconds 86400000 in a day and then format with d3
39
#on lineWithFocusChart type xAxis will also set x2Axis unless it is specified
40
p7$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )
41
#test xAxis also sets x2Axis
42
p7
43
#now test setting x2Axis to something different
44
#test format dates on the x2Axis
45
#test to show %Y format which is different than xAxis
46
p7$x2Axis( tickFormat="#!function(d) {return d3.time.format('%Y')(new Date( d * 86400000 ));}!#" )
47
p7
48
#test set xAxis again to make sure it does not override set x2Axis
49
p7$xAxis( NULL, replace = T)
50
p7
51
52
## {title: Stacked Area Chart}
53
dat <- data.frame(t=rep(0:23,each=4),var=rep(LETTERS[1:4],4),val=round(runif(4*24,0,50)))
54
p8 <- nPlot(val ~ t, group = 'var', data = dat, type = 'stackedAreaChart', id = 'chart')
55
p8
56
57
58
## {title: InteractiveGuidline(Multi-Tooltips) on Line}
59
p9 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineChart')
60
p9$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )
61
#try new interactive guidelines feature
62
p9$chart(useInteractiveGuideline=TRUE)
63
p9
64
65
66
## {title: InteractiveGuidline(Multi-Tooltips) on Stack}
67
p10 <- p8
68
p10$chart(useInteractiveGuideline=TRUE)
69
p10
70
71
## {title: showDistX and showDistY}
72
p11 <- p1
73
p11$chart(showDistX = TRUE, showDistY = TRUE)
74
p11
75
76
## {title: multiChart}
77
p12 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'multiChart')
78
p12$params$multi = list(
79
uempmed = list(type="area",yAxis=1),
80
psavert = list(type="line",yAxis=2)
81
)
82
p12$setTemplate(script = system.file(
83
"/libraries/nvd3/layouts/multiChart.html",
84
package = "rCharts"
85
))
86
p12
87
88
## {title: Facets}
89
p13 <- nPlot(mpg ~ wt, data = mtcars, group = "gear", type = "scatterChart")
90
p13$params$facet = "cyl"
91
p13$templates$script = system.file(
92
"/libraries/nvd3/layouts/nvd3FacetPlot.html",
93
package = "rCharts"
94
)
95
p13
96
97
hair_eye = as.data.frame(HairEyeColor)
98
p14 <- nPlot(Freq ~ Hair, group = 'Sex', data = hair_eye, type = 'multiBarChart')
99
p14$params$facet="Eye"
100
p14$templates$script = system.file(
101
"/libraries/nvd3/layouts/nvd3FacetPlot.html",
102
package = "rCharts"
103
)
104
p14
105
106
p15 <- nPlot(Freq ~ Hair, group = 'Eye', data = hair_eye, type = 'multiBarChart')
107
p15$params$facet="Sex"
108
p15$templates$script = system.file(
109
"/libraries/nvd3/layouts/nvd3FacetPlot.html",
110
package = "rCharts"
111
)
112
p15
113
114
115
## {title: Sparklines}
116
p16 <- nPlot(uempmed ~ date, data = economics, type = 'sparklinePlus',height=100,width=500)
117
p16$chart(xTickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#")
118
p16
119
## semi replicate sparkline with a full nvd3 model by setting short height and turning off lots of things
120
p17 <- nPlot(
121
x = "date",
122
y = "volume",
123
data = spy.df,
124
type = "multiBarChart",
125
height = 200)
126
p17$chart(showControls = FALSE, showLegend = FALSE, showXAxis = FALSE, showYAxis = FALSE)
127
p17$xAxis(tickFormat =
128
"#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#"
129
)
130
p17
131
132
133
## {title: ohlcBar}
134
## ohlcBar not fully implemented on nvd3 side, so no axes or interactive controls
135
## note do not melt if using ohlcBar
136
require(quantmod)
137
138
spy <- getSymbols("SPY",auto.assign=FALSE,from="2013-01-01")
139
colnames(spy) <- c("open","high","low","close","volume","adjusted")
140
141
spy.df <- data.frame(index(spy),spy)
142
colnames(spy.df)[1] <- "date"
143
144
p18 <- nPlot(
145
x = "date",
146
y = "close",
147
data = spy.df,
148
type = "ohlcBar"
149
)
150
p18
151
152