| Title: | An R 'graphics'-like Interface to 'Chart.js' |
|---|---|
| Description: | A continuation of the author's earlier 'c3plot' project, create interactive plots with the 'Chart.js' <https://www.chartjs.org/> charting library using an interface modeled after that of base graphics. The main plotjs() function is an S3 generic with methods for various R objects that work similarly to their equivalent plot() methods. These plots can be embedded into 'Shiny' web applications or 'R Markdown' documents or displayed within the 'RStudio' 'Viewer' pane. |
| Authors: | Alex Kraieski [aut, cre], R Core team [ctb] (Some code and examples extracted from base R 'plot()' methods) |
| Maintainer: | Alex Kraieski <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0 |
| Built: | 2026-05-19 05:47:06 UTC |
| Source: | https://github.com/arkraieski/plotjs |
Creates a bar plot using 'Chart.js'.
jsbarplot( heights, names.arg = NULL, col = NULL, main = NULL, ylab = NULL, width = NULL, height = NULL, elementId = NULL, aria.label = NULL, ... )jsbarplot( heights, names.arg = NULL, col = NULL, main = NULL, ylab = NULL, width = NULL, height = NULL, elementId = NULL, aria.label = NULL, ... )
heights |
a vector of values describing the bars that make up the plot. |
names.arg |
a vector of names to be plotted below each bar. |
col |
a character string with the color for bars. This can be either a hex value or the name of an R built-in color. |
main |
a main title for the plot. |
ylab |
a label for the y axis. |
width |
width of the widget to create for the plot. The default is NULL, which results in automatic resizing based on the plot's container. |
height |
height of the widget to create for the plot. The default is NULL, which results in automatic resizing based on the plot's container. |
elementId |
Use an explicit element ID for the widget, rather than an automatically generated one. |
aria.label |
a character string set as the |
... |
arguments passed from methods. |
Output and render functions for using jsbarplot within Shiny applications and interactive Rmd documents.
jsbarplotOutput(outputId, width = "100%", height = "400px") renderJsbarplot(expr, env = parent.frame(), quoted = FALSE)jsbarplotOutput(outputId, width = "100%", height = "400px") renderJsbarplot(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 jsbarplot |
env |
The environment in which to evaluate |
quoted |
Is |
Draw an interactive pie chart using 'Chart.js'
jspie( x, labels = names(x), col = NULL, slice.text = "pct", donut = FALSE, legend.position = "right", main = NULL, width = NULL, height = NULL, elementId = NULL, aria.label = NULL )jspie( x, labels = names(x), col = NULL, slice.text = "pct", donut = FALSE, legend.position = "right", main = NULL, width = NULL, height = NULL, elementId = NULL, aria.label = NULL )
x |
a vector of non-negative numerical quantities. The values in x are displayed as the areas of pie slices. |
labels |
character vector giving names for the slices. |
col |
character vector of colors to be used in filling the slices. Can be a hex value or an R built-in color name. |
slice.text |
|
donut |
logical; should the chart be rendered as a donut chart instead of a full pie? |
legend.position |
Position of the legend. Possible values are |
main |
a main title for the plot. |
width |
width of the widget to create for the plot. The default is NULL, which results in automatic resizing based on the plot's container. |
height |
height of the widget to create for the plot. The default is NULL, which results in automatic resizing based on the plot's container. |
elementId |
Use an explicit element ID for the widget, rather than an automatically generated one. |
aria.label |
a character string set as the |
pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12) names(pie.sales) <- c("Blueberry", "Cherry", "Apple", "Boston Cream", "Other", "Vanilla Cream") jspie(pie.sales) jspie(pie.sales, col = c("purple", "violetred1", "green3", "cornsilk", "cyan", "white")) jspie(pie.sales, donut = TRUE)pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12) names(pie.sales) <- c("Blueberry", "Cherry", "Apple", "Boston Cream", "Other", "Vanilla Cream") jspie(pie.sales) jspie(pie.sales, col = c("purple", "violetred1", "green3", "cornsilk", "cyan", "white")) jspie(pie.sales, donut = TRUE)
Output and render functions for using jspie within Shiny applications and interactive Rmd documents.
jspieOutput(outputId, width = "100%", height = "400px") renderJspie(expr, env = parent.frame(), quoted = FALSE)jspieOutput(outputId, width = "100%", height = "400px") renderJspie(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 jspie |
env |
The environment in which to evaluate |
quoted |
Is |
This is a generic function for plotting 'R' objects using the Chart.js charting library. The syntax is similar to that of the plot() generic function.
plotjs(x, ...)plotjs(x, ...)
x |
x coordinates for points or any 'R' object with a |
... |
arguments passed to methods. |
For simple scatter and line plots, plotjs.default will be used. However, there are plotjs methods for various 'R' objects. Use methods(plotjs) and the documentation for these.
Plots created with this are interactive htmlwidgets that can be used in the RStudio Viewer or embedded into 'R Markdown' documents, 'Shiny' web applications, etc.
For the default method, the argument y with the y coordinates of points is required. For some methods, such as plotjs.density and plotjs.function, y is not required because those methods can compute or extract y coordinates from the x object.
data(mtcars) plotjs(mtcars$disp, mtcars$hp, main = "Displacement vs. HP in mtcars") plotjs(qnorm)data(mtcars) plotjs(mtcars$disp, mtcars$hp, main = "Displacement vs. HP in mtcars") plotjs(qnorm)
Output and render functions for using plotjs within Shiny applications and interactive Rmd documents.
plotjsOutput(outputId, width = "100%", height = "400px") renderPlotjs(expr, env = parent.frame(), quoted = FALSE)plotjsOutput(outputId, width = "100%", height = "400px") renderPlotjs(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 plotjsScatter |
env |
The environment in which to evaluate |
quoted |
Is |
Draw an interactive scatterplot or line plot using 'Chart.js'.
## Default S3 method: plotjs( x, y, type = "p", xlim = NULL, ylim = NULL, main = NULL, xlab = NULL, ylab = NULL, zoom = TRUE, col.group = NULL, col = NULL, legend.title = NULL, legend.position = "right", sci.x = FALSE, sci.y = FALSE, ... )## Default S3 method: plotjs( x, y, type = "p", xlim = NULL, ylim = NULL, main = NULL, xlab = NULL, ylab = NULL, zoom = TRUE, col.group = NULL, col = NULL, legend.title = NULL, legend.position = "right", sci.x = FALSE, sci.y = FALSE, ... )
x |
the x coordinates for the plot. |
y |
the y coordinates for the plot. |
type |
1-character string giving the type of plot desired. The following values are possible: "p" for points, "l" for lines, and "b" for both points and lines. |
xlim |
the x limits (x1, x2) of the plot. |
ylim |
the y limits of the plot. |
main |
a main title for the plot. |
xlab |
a label for the x axis, defaults to a description of x. |
ylab |
a label for the y axis, defaults to a description of y. |
zoom |
logical; should the zooming feature be enabled for the plot? |
col.group |
optionally, a factor the same length as |
col |
The colors for the lines and points. If |
legend.title |
a title for the legend. Defaults to a description of |
legend.position |
Position of the plot legend. Possible values are "right", "bottom", "inset", and "hide". This is ignored if all points are colored the same. |
sci.x |
logical indicating whether scientific notation should be used for the x-axis. |
sci.y |
logical indicating whether scientific notation should be used for the y-axis. |
... |
arguments passed to |
data(mtcars) plotjs(mtcars$hp, mtcars$qsec) plotjs(mtcars$disp, mtcars$hp, main = "Displacement vs. HP in mtcars")data(mtcars) plotjs(mtcars$hp, mtcars$qsec) plotjs(mtcars$disp, mtcars$hp, main = "Displacement vs. HP in mtcars")
The plotjs method for density objects.
## S3 method for class 'density' plotjs(x, main = NULL, xlab = NULL, ylab = "Density", type = "l", ...)## S3 method for class 'density' plotjs(x, main = NULL, xlab = NULL, ylab = "Density", type = "l", ...)
x |
a "density" object |
main |
a main title for the plot. |
xlab |
label for the x axis |
ylab |
a label for the y axis, defaults to a description of y. |
type |
1-character string giving the type of plot desired. The following values are possible: "p" for points, "l" for lines, and "b" for both points and lines. |
... |
arguments passed to other methods |
This function implements a bar plot method for factor arguments to the plotjs generic function. Bar heights will be counts for the factor levels.
## S3 method for class 'factor' plotjs(x, ylab = "Count", ...)## S3 method for class 'factor' plotjs(x, ylab = "Count", ...)
x |
a factor. |
ylab |
a label for the y axis. |
... |
arguments passed to |
mtcars <- mtcars mtcars$cyl <- as.factor(mtcars$cyl) plotjs(mtcars$cyl)mtcars <- mtcars mtcars$cyl <- as.factor(mtcars$cyl) plotjs(mtcars$cyl)
Draws a curve corresponding to a function over the interval [from, to] in an interactive plot using 'Chart.js'.
## S3 method for class ''function'' plotjs(x, from = 0, to = 1, ylab = NULL, ...)## S3 method for class ''function'' plotjs(x, from = 0, to = 1, ylab = NULL, ...)
x |
The name of a function. |
from |
the lower limit of the range over which the function will be plotted. |
to |
the upper limit of the range over which the function will be plotted. |
ylab |
a label for the y axis, defaults to the name of the function specified in |
... |
arguments passed to other methods |
plotjs(qnorm) plotjs(sin, -pi, 2 * pi)plotjs(qnorm) plotjs(sin, -pi, 2 * pi)
A plot of residuals against fitted values.
## S3 method for class 'lm' plotjs(x, which = 1, ...)## S3 method for class 'lm' plotjs(x, which = 1, ...)
x |
an "lm" object |
which |
1 is the only supported value currently. This argument is included for consistency with the base |
... |
arguments passed to other methods. |
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) plotjs(lm.SR)lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) plotjs(lm.SR)