D3.js can draw almost
any chart, but it has a steep learning curve and asks for some
HTML, CSS and JavaScript.
ddplot wraps a collection of D3 charts in
plain R functions: you pass a data frame and column names,
and you get an interactive SVG graphic that works in the RStudio viewer,
R Markdown, Quarto and Shiny.
ddplot is built on the r2d3 package, which does the
heavy lifting of connecting R and D3. Thanks
to its developers.
Every function takes a data frame and the names of the columns to plot, as strings.
line_chart() accepts a ts object directly.
Hover over the chart to read the values, drag on it to zoom, or use the
range buttons and the navigator below it:
A multivariate ts gives one line per column. With a data
frame, give the date column as x and one or several value
columns as y (or a single value column and a
group column):
Bar charts take one row per category, or raw rows that they count or
aggregate for you (agg). Here the data is aggregated first
with dplyr:
ddplot has 60 chart types. The gallery
shows all of them, and the guides below walk through each one with
examples, starting with the charts you are most likely to need:
| Guide | Charts |
|---|---|
| Time series and trends | line_chart(), area_chart(),
stacked_area_chart(), area_band(),
streamgraph(), calendar_heatmap(),
anim_line_chart() |
| Comparing categories | bar_chart(), horz_bar_chart(),
lollipop_chart(), horz_lollipop(),
dumbbell_chart(), slope_chart(),
bump_chart(), waterfall_chart(),
radar_chart(), radial_bar_chart() |
| Distributions | histogram(), violin_plot(),
beeswarm_plot(), ridgeline_plot(),
animated_histogram() |
| Relationships and flows | scatter_plot(), hexbin_plot(),
chord_diagram() |
| Part-to-whole and hierarchies | pie_chart(), treemap_chart(),
waffle_chart(), sunburst_chart(),
mosaic_chart(), rose_chart(),
parliament_chart() |
| Race charts | bar_chart_race(), line_race(),
bubble_race(), radial_bar_race(),
treemap_race(), donut_race() |
| KPIs and monitoring | gauge_chart(), bullet_chart(),
liquid_chart(), pulse_grid() |
| Playful indicators | heart_fill(), glass_fill(),
plant_growth(), flame(),
flower() |
ddplot in ShinyEvery ddplot chart is an r2d3 htmlwidget,
so in a Shiny app you can use r2d3::d3Output()
in the UI and r2d3::renderD3() in the server. When an input
changes, the chart is redrawn in place with the new data and options.
Interactive state, such as the zoom of a line_chart() or
the series hidden from its legend, is kept between updates.
library(shiny)
library(ddplot)
library(r2d3)
ui <- fluidPage(
h2("Example ddplot application"),
selectInput(
inputId = "colors",
label = "Point color",
choices = colors(),
selected = "springgreen3"
),
sliderInput(
inputId = "slider",
label = "Point size",
min = 1,
max = 10,
value = 3
),
d3Output("scatter", height = "450px")
)
server <- function(input, output) {
output$scatter <- renderD3({
scatter_plot(
data = iris,
x = "Sepal.Length",
y = "Sepal.Width",
size = input$slider,
col = input$colors
)
})
}
shinyApp(ui = ui, server = server)