Create¶
The NVD3 library supports most of the common chart types.
You can create an interactive plot making use of the NVD3 library using the nPlot() function.
Argument | Type | Description |
---|---|---|
x | formula | A formula of the form y ~ x, with column names from the data frame. |
data | data frame | A data frame containing the data to be plotted |
type | string | The type of chart to plot |
group | string | Name of column based on which data should be grouped. |
Scatter Chart¶
p1 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p1$xAxis(axisLabel = 'Weight')
p1
Multibar Chart¶
hair_eye = as.data.frame(HairEyeColor)
p2 <- nPlot(Freq ~ Hair, group = 'Eye',
data = subset(hair_eye, Sex == "Female"),
type = 'multiBarChart'
)
p2$chart(color = c('brown', 'blue', '#594c26', 'green'))
p2
Line Chart¶
data(economics, package = 'ggplot2')
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6
Line with Focus Chart¶
ecm <- reshape2::melt(
economics[,c('date', 'uempmed', 'psavert')],
id = 'date'
)
p7 <- nPlot(value ~ date, group = 'variable',
data = ecm,
type = 'lineWithFocusChart'
)
p7
Stacked Area Chart¶
dat <- data.frame(
t = rep(0:23, each = 4),
var = rep(LETTERS[1:4], 4),
val = round(runif(4*24,0,50))
)
p8 <- nPlot(val ~ t, group = 'var', data = dat,
type = 'stackedAreaChart', id = 'chart'
)
p8
Multi Chart¶
p12 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'multiChart')
p12$set(multi = list(
uempmed = list(type="area", yAxis=1),
psavert = list(type="line", yAxis=2)
))
p12$setTemplate(script = system.file(
"/libraries/nvd3/layouts/multiChart.html",
package = "rCharts"
))
p12