Title: | An Incomplete Wrapper of the 'nvd3' JavaScript Library |
---|---|
Description: | Creates JavaScript charts with the 'nvd3' library. So far only the multibar chart, the horizontal multibar chart, the line chart and the line chart with focus are available. |
Authors: | Stéphane Laurent [aut, cre], Novus Partners, Inc. [cph] ('nvd3.js' library), Michael Bostock [cph] ('d3.js' library) |
Maintainer: | Stéphane Laurent <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-10-29 02:45:12 UTC |
Source: | https://github.com/stla/rnvd3 |
HTMLwidget displaying a horizontal multibar chart.
hMultiBarChart( data, formula, by, palette = "viridis", xAxisTitle = NULL, yAxisTitle = NULL, margins = list(b = 100, l = 100), duration = 1300, groupSpacing = 0.1, xAxisTitleDistance = 25, yAxisTitleDistance = -5, yAxisShowMaxMin = FALSE, yAxisTickFormat = ".0f", nticks = 5, xLabelsFontSize = "1rem", yLabelsFontSize = "1rem", showValues = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
hMultiBarChart( data, formula, by, palette = "viridis", xAxisTitle = NULL, yAxisTitle = NULL, margins = list(b = 100, l = 100), duration = 1300, groupSpacing = 0.1, xAxisTitleDistance = 25, yAxisTitleDistance = -5, yAxisShowMaxMin = FALSE, yAxisTickFormat = ".0f", nticks = 5, xLabelsFontSize = "1rem", yLabelsFontSize = "1rem", showValues = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
data |
dataframe containing the data used for the chart |
formula |
a two-sided formula like |
by |
string, the "by" variable; must be a column name of |
palette |
this can be either the name of a viridis color palette, e.g.
|
xAxisTitle |
a title for the x-axis; if |
yAxisTitle |
a title for the y-axis; if |
margins |
a named list defining the margins, with names |
duration |
duration of the transition, a number of milliseconds |
groupSpacing |
a number, controls the distance between groups of bars |
xAxisTitleDistance |
a number, controls the distance between the x-axis and its title |
yAxisTitleDistance |
a number, controls the distance between the y-axis and its title |
yAxisShowMaxMin |
Boolean, whether to show the min and the max on the y-axis |
yAxisTickFormat |
a d3 formatting string for the y-axis; see d3.format |
nticks |
integer, the number of ticks on the y-axis |
xLabelsFontSize |
a CSS measure, the font size of the labels on the x-axis |
yLabelsFontSize |
a CSS measure, the font size of the labels on the y-axis |
showValues |
Boolean, whether to show the values next to the bars |
tooltipFormatters |
formatters for the tooltip; each formatter must
be
|
tooltipTransitions |
Boolean, whether to style the tooltip with a fade effect |
tooltipShadow |
Boolean, whether to style the tooltip with a shadow |
width |
width of the chart container, must be a valid CSS measure |
height |
height of the chart container, must be a valid CSS measure |
elementId |
an id for the chart container; commonly useless |
A htmlwidget displaying a grouped/stacked bar chart.
library(Rnvd3) dat <- aggregate(breaks ~ wool + tension, data = warpbreaks, mean) levels(dat[["tension"]]) <- c("Low", "Medium", "High") hMultiBarChart( dat, breaks ~ wool, "tension", yAxisShowMaxMin = TRUE, yAxisTitle = "Mean of breaks", yAxisTickFormat = ".01f" ) # the axis titles are small, let's make them bigger library(htmltools) CSS <- HTML( ".nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 1rem; }" ) prependContent( hMultiBarChart( dat, breaks ~ wool, "tension", yAxisShowMaxMin = TRUE, yAxisTitle = "Mean of breaks", yAxisTickFormat = ".01f" ), tags$style(CSS) )
library(Rnvd3) dat <- aggregate(breaks ~ wool + tension, data = warpbreaks, mean) levels(dat[["tension"]]) <- c("Low", "Medium", "High") hMultiBarChart( dat, breaks ~ wool, "tension", yAxisShowMaxMin = TRUE, yAxisTitle = "Mean of breaks", yAxisTickFormat = ".01f" ) # the axis titles are small, let's make them bigger library(htmltools) CSS <- HTML( ".nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 1rem; }" ) prependContent( hMultiBarChart( dat, breaks ~ wool, "tension", yAxisShowMaxMin = TRUE, yAxisTitle = "Mean of breaks", yAxisTickFormat = ".01f" ), tags$style(CSS) )
Create a HTML widget displaying a line chart.
lineChart( data, xAxisTitle = "x", yAxisTitle = "y", margins = list(l = 90), duration = 500, useInteractiveGuideline = TRUE, xAxisTickFormat = ".0f", yAxisTickFormat = ".02f", xLabelsFontSize = "0.75rem", yLabelsFontSize = "0.75rem", legendPosition = "top", interpolate = "linear", xRange = NULL, yRange = NULL, rightAlignYaxis = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
lineChart( data, xAxisTitle = "x", yAxisTitle = "y", margins = list(l = 90), duration = 500, useInteractiveGuideline = TRUE, xAxisTickFormat = ".0f", yAxisTickFormat = ".02f", xLabelsFontSize = "0.75rem", yLabelsFontSize = "0.75rem", legendPosition = "top", interpolate = "linear", xRange = NULL, yRange = NULL, rightAlignYaxis = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
data |
data used for the chart; it must be a list created with
|
xAxisTitle |
string, the title of the x-axis |
yAxisTitle |
string, the title of the y-axis |
margins |
a named list defining the margins, with names |
duration |
transition duration in milliseconds |
useInteractiveGuideline |
Boolean, a guideline and synchronized tooltips |
xAxisTickFormat |
a d3 formatting string for the ticks on the x-axis; a d3 time formatting string if the x-values are dates, see d3.time.format |
yAxisTickFormat |
a d3 formatting string for the ticks on the y-axis |
xLabelsFontSize |
a CSS measure, the font size of the labels on the x-axis |
yLabelsFontSize |
a CSS measure, the font size of the labels on the y-axis |
legendPosition |
string, the legend position, |
interpolate |
interpolation type, a string among |
xRange |
the x-axis range, a length two vector of the same type as the
x-values, or |
yRange |
the y-axis range, a numeric vector of length 2, or
|
rightAlignYaxis |
Boolean, whether to put the y-axis on the right side instead of the left |
tooltipFormatters |
formatters for the tooltip; each formatter must
be
|
tooltipTransitions |
Boolean, whether to style the tooltip with a fade effect |
tooltipShadow |
Boolean, whether to style the tooltip with a shadow |
width |
width of the chart container, must be a valid CSS measure |
height |
height of the chart container, must be a valid CSS measure |
elementId |
an id for the chart container, usually useless |
A HTML widget displaying a line chart.
library(Rnvd3) dat1 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "red") dat <- list(dat1, dat2) lineChart(dat) # with a date x-axis #### dat1 <- lineChartData( x = ~ Sys.Date() + 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime" ) dat2 <- lineChartData(x = ~ Sys.Date() + 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "darkred") dat <- list(dat1, dat2) lineChart( dat, margins = list(t = 100, r = 100, b = 100, l = 100), xAxisTickFormat = "%Y-%m-%d" ) # with a datetime x-axis dat <- data.frame( x = Sys.time() + (1:300), y1 = sin(1:300/10), y2 = sin(1:300/10)*0.25 + 0.5 ) dat1 <- lineChartData(x = ~x, y = ~y1, data = dat, key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~x, y = ~y2, data = dat, key = "Another sine wave", color = "darkred") dat12 <- list(dat1, dat2) lineChart( dat12, margins = list(t = 100, r = 100, b = 100, l = 100), xAxisTickFormat = "%H:%M:%S", xAxisTitle = "Time", yAxisTitle = "Energy" )
library(Rnvd3) dat1 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "red") dat <- list(dat1, dat2) lineChart(dat) # with a date x-axis #### dat1 <- lineChartData( x = ~ Sys.Date() + 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime" ) dat2 <- lineChartData(x = ~ Sys.Date() + 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "darkred") dat <- list(dat1, dat2) lineChart( dat, margins = list(t = 100, r = 100, b = 100, l = 100), xAxisTickFormat = "%Y-%m-%d" ) # with a datetime x-axis dat <- data.frame( x = Sys.time() + (1:300), y1 = sin(1:300/10), y2 = sin(1:300/10)*0.25 + 0.5 ) dat1 <- lineChartData(x = ~x, y = ~y1, data = dat, key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~x, y = ~y2, data = dat, key = "Another sine wave", color = "darkred") dat12 <- list(dat1, dat2) lineChart( dat12, margins = list(t = 100, r = 100, b = 100, l = 100), xAxisTickFormat = "%H:%M:%S", xAxisTitle = "Time", yAxisTitle = "Energy" )
Make line chart data.
lineChartData(x, y, data = NULL, key, color, area = FALSE)
lineChartData(x, y, data = NULL, key, color, area = FALSE)
x |
a right-sided formula giving the variable on the x-axis, numeric or date type |
y |
a right-sided formula giving the variable on the x-axis, numeric type |
data |
dataframe containing the data for the chart; if not |
key |
string, the title of the line chart |
color |
string, the color of the line chart |
area |
Boolean, whether to turn the line chart into a filled area chart |
A list, for usage in lineChart
.
The color can be given by the name of a R color, the name of a CSS
color, e.g. "lime"
or "fuchsia"
, an HEX code like
"#ff009a"
, a RGB code like "rgb(255,100,39)"
, or a HSL code
like "hsl(360,11,255)"
.
Create a HTML widget displaying a line chart with a focus tool.
lineFocusChart( data, xAxisTitle = "x", yAxisTitle = "y", margins = list(l = 90), duration = 500, useInteractiveGuideline = FALSE, xAxisTickFormat = ".0f", yAxisTickFormat = ".02f", xLabelsFontSize = "0.75rem", yLabelsFontSize = "0.75rem", legendPosition = "top", interpolate = "linear", xRange = NULL, yRange = NULL, rightAlignYaxis = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
lineFocusChart( data, xAxisTitle = "x", yAxisTitle = "y", margins = list(l = 90), duration = 500, useInteractiveGuideline = FALSE, xAxisTickFormat = ".0f", yAxisTickFormat = ".02f", xLabelsFontSize = "0.75rem", yLabelsFontSize = "0.75rem", legendPosition = "top", interpolate = "linear", xRange = NULL, yRange = NULL, rightAlignYaxis = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, width = "100%", height = NULL, elementId = NULL )
data |
data used for the chart; it must be a list created with
|
xAxisTitle |
string, the title of the x-axis |
yAxisTitle |
string, the title of the y-axis |
margins |
a named list defining the margins, with names |
duration |
transition duration in milliseconds |
useInteractiveGuideline |
Boolean, a guideline and synchronized tooltips |
xAxisTickFormat |
a d3 formatting string for the ticks on the x-axis; a d3 time formatting string if the x-values are dates, see d3.time.format |
yAxisTickFormat |
a d3 formatting string for the ticks on the y-axis |
xLabelsFontSize |
a CSS measure, the font size of the labels on the x-axis |
yLabelsFontSize |
a CSS measure, the font size of the labels on the y-axis |
legendPosition |
string, the legend position, |
interpolate |
interpolation type, a string among |
xRange |
the x-axis range, a length two vector of the same type as the
x-values, or |
yRange |
the y-axis range, a numeric vector of length 2, or
|
rightAlignYaxis |
Boolean, whether to put the y-axis on the right side instead of the left |
tooltipFormatters |
formatters for the tooltip; each formatter must
be
|
tooltipTransitions |
Boolean, whether to style the tooltip with a fade effect |
tooltipShadow |
Boolean, whether to style the tooltip with a shadow |
width |
width of the chart container, must be a valid CSS measure |
height |
height of the chart container, must be a valid CSS measure |
elementId |
an id for the chart container, usually useless |
A HTML widget displaying a line chart with a focus tool.
library(Rnvd3) dat1 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "red") dat <- list(dat1, dat2) lineFocusChart(dat)
library(Rnvd3) dat1 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10), key = "Sine wave", color = "lime") dat2 <- lineChartData(x = ~ 1:100, y = ~ sin(1:100/10)*0.25 + 0.5, key = "Another sine wave", color = "red") dat <- list(dat1, dat2) lineFocusChart(dat)
HTMLwidget displaying a multibar chart.
multiBarChart( data, formula, by, palette = "viridis", xAxisTitle = NULL, yAxisTitle = NULL, margins = list(b = 100, l = 70), duration = 1300, rotateLabels = 0, groupSpacing = 0.1, xAxisTitleDistance = 35, yAxisTitleDistance = -5, yAxisShowMaxMin = FALSE, yAxisTickFormat = ".0f", xLabelsFontSize = "1rem", yLabelsFontSize = "1rem", rightAlignYaxis = FALSE, reduceXticks = FALSE, staggerLabels = FALSE, wrapLabels = FALSE, useInteractiveGuideline = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, radioButtonMode = FALSE, legendTitle = NULL, legendHjust = -20, width = "100%", height = NULL, elementId = NULL )
multiBarChart( data, formula, by, palette = "viridis", xAxisTitle = NULL, yAxisTitle = NULL, margins = list(b = 100, l = 70), duration = 1300, rotateLabels = 0, groupSpacing = 0.1, xAxisTitleDistance = 35, yAxisTitleDistance = -5, yAxisShowMaxMin = FALSE, yAxisTickFormat = ".0f", xLabelsFontSize = "1rem", yLabelsFontSize = "1rem", rightAlignYaxis = FALSE, reduceXticks = FALSE, staggerLabels = FALSE, wrapLabels = FALSE, useInteractiveGuideline = FALSE, tooltipFormatters = list(value = NULL, header = NULL, key = NULL), tooltipTransitions = TRUE, tooltipShadow = TRUE, radioButtonMode = FALSE, legendTitle = NULL, legendHjust = -20, width = "100%", height = NULL, elementId = NULL )
data |
dataframe used for the chart |
formula |
a two-sided formula like |
by |
string, the "by" variable; must be a column name of |
palette |
this can be either the name of a viridis color palette, e.g.
|
xAxisTitle |
a title for the x-axis; if |
yAxisTitle |
a title for the y-axis; if |
margins |
a named list defining the margins, with names |
duration |
duration of the transition, a number of milliseconds |
rotateLabels |
a number, the angle of rotation of the labels of the x-axis (in degrees) |
groupSpacing |
a number, controls the distance between groups of bars |
xAxisTitleDistance |
a number, controls the distance between the x-axis and its title |
yAxisTitleDistance |
a number, controls the distance between the y-axis and its title |
yAxisShowMaxMin |
Boolean, whether to show the min and the max on the y-axis |
yAxisTickFormat |
a d3 formatting string for the y-axis; see d3.format |
xLabelsFontSize |
a CSS measure, the font size of the labels on the x-axis |
yLabelsFontSize |
a CSS measure, the font size of the labels on the y-axis |
rightAlignYaxis |
Boolean, whether to put the y-axis on the right side instead of the left |
reduceXticks |
Boolean, whether to reduce the ticks on the x-axis so that the x-labels are less likely to overlap |
staggerLabels |
Boolean, whether to make the x-labels stagger at different distances from the axis so they're less likely to overlap |
wrapLabels |
Boolean, whether to split long x-labels by new lines in order to prevent overlapping |
useInteractiveGuideline |
Boolean, other kind of tooltips: sets the chart to use a guideline and floating tooltip instead of requiring the user to hover over specific hotspots |
tooltipFormatters |
formatters for the tooltip; each formatter must
be
|
tooltipTransitions |
Boolean, whether to style the tooltip with a fade effect |
tooltipShadow |
Boolean, whether to style the tooltip with a shadow |
radioButtonMode |
Boolean, whether to authorize only one selection in
the legend (i.e. only one level of the ' |
legendTitle |
a title for the legend, or |
legendHjust |
horizontal adjustment of the legend title |
width |
width of the chart container, must be a valid CSS measure |
height |
height of the chart container, must be a valid CSS measure |
elementId |
an id for the chart container; commonly useless |
A htmlwidget displaying a grouped/stacked bar chart.
In Shiny, you can style the axis titles with the help of CSS; see the
shiny example. It is also possible outside of Shiny;
see the second example below, where we set the CSS with the help of
prependContent
.
library(Rnvd3) # in this example we use the tooltip formatters for styling only; we could # achieve the same result with the help of CSS dat <- reshape2::melt( apply(HairEyeColor, c(1, 2), sum), value.name = "Count" ) multiBarChart( dat, Count ~ Eye, "Hair", tooltipFormatters = list( value = JS( "function(x){", " return '<span style=\"color:red;\">' + x + '</span>';", "}" ), header = JS( "function(x){", " return '<span style=\"color:green;\">' + x + '</span>';", "}" ), key = JS( "function(x){", " return '<i style=\"color:blue;\">' + x + '</i>';", "}" ) ) ) # style axis titles with CSS #### library(htmltools) CSS <- HTML( ".nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 2rem; fill: red; }" ) widget <- multiBarChart( dat, Count ~ Eye, "Hair", palette = "turbo" ) prependContent( widget, tags$style(CSS) )
library(Rnvd3) # in this example we use the tooltip formatters for styling only; we could # achieve the same result with the help of CSS dat <- reshape2::melt( apply(HairEyeColor, c(1, 2), sum), value.name = "Count" ) multiBarChart( dat, Count ~ Eye, "Hair", tooltipFormatters = list( value = JS( "function(x){", " return '<span style=\"color:red;\">' + x + '</span>';", "}" ), header = JS( "function(x){", " return '<span style=\"color:green;\">' + x + '</span>';", "}" ), key = JS( "function(x){", " return '<i style=\"color:blue;\">' + x + '</i>';", "}" ) ) ) # style axis titles with CSS #### library(htmltools) CSS <- HTML( ".nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 2rem; fill: red; }" ) widget <- multiBarChart( dat, Count ~ Eye, "Hair", palette = "turbo" ) prependContent( widget, tags$style(CSS) )
These objects are imported from other packages.
Follow the links to their documentation:
JS
,
saveWidget
,
prependContent
.
Output and render functions for using rnvd3
widgets
within Shiny applications and interactive Rmd documents.
rnvd3Output(outputId, width = "100%", height = "400px") renderRnvd3(expr, env = parent.frame(), quoted = FALSE)
rnvd3Output(outputId, width = "100%", height = "400px") renderRnvd3(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width , height
|
must be a valid CSS unit (like |
expr |
an expression that generates a |
env |
the environment in which to evaluate |
quoted |
is |
rnvd3Output
returns an output element that can be included
in a Shiny UI definition, and renderRnvd3
returns a
shiny.render.function
object that can be included in a Shiny server
definition.
library(Rnvd3) library(shiny) dat <- reshape2::melt( apply(HairEyeColor, c(1, 2), sum), value.name = "Count" ) CSS <- HTML( "body { overflow: overlay; } /* style axis titles */ .nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 3rem; fill: red; } /* style the tooltip */ .nvtooltip .value { color: red; } .nvtooltip .x-value { color: green; } .nvtooltip .key { color: blue; font-style: italic; } " ) ui <- fluidPage( tags$head(tags$style(CSS)), br(), fluidRow( column( 9, rnvd3Output("mychart", width = "100%", height = "500px") ), column( 3, tags$h3("Chart state:"), verbatimTextOutput("state") ) ) ) server <- function(input, output, session){ output[["mychart"]] <- renderRnvd3({ multiBarChart( dat, Count ~ Eye, "Hair", palette = "viridis", xLabelsFontSize = "2rem", yLabelsFontSize = "2rem" ) }) output[["state"]] <- renderPrint({ input[["mychart_state"]] }) } if(interactive()){ shinyApp(ui, server) }
library(Rnvd3) library(shiny) dat <- reshape2::melt( apply(HairEyeColor, c(1, 2), sum), value.name = "Count" ) CSS <- HTML( "body { overflow: overlay; } /* style axis titles */ .nvd3 .nv-axis.nv-x text.nv-axislabel, .nvd3 .nv-axis.nv-y text.nv-axislabel { font-size: 3rem; fill: red; } /* style the tooltip */ .nvtooltip .value { color: red; } .nvtooltip .x-value { color: green; } .nvtooltip .key { color: blue; font-style: italic; } " ) ui <- fluidPage( tags$head(tags$style(CSS)), br(), fluidRow( column( 9, rnvd3Output("mychart", width = "100%", height = "500px") ), column( 3, tags$h3("Chart state:"), verbatimTextOutput("state") ) ) ) server <- function(input, output, session){ output[["mychart"]] <- renderRnvd3({ multiBarChart( dat, Count ~ Eye, "Hair", palette = "viridis", xLabelsFontSize = "2rem", yLabelsFontSize = "2rem" ) }) output[["state"]] <- renderPrint({ input[["mychart_state"]] }) } if(interactive()){ shinyApp(ui, server) }