install.packages(
"rtemis.draw",
repos = c("https://rtemis-org.r-universe.dev", "https://cloud.r-project.org")
)53 Introduction to rtemis.draw
rtemis.draw is a plotting package from the rtemis project. Like plotly (Chapter 52), it produces interactive htmlwidgets, but it provides data-first draw_*() functions that take plain vectors, lists, or data frames and choose sensible defaults, so most plots take a single call. Charts are rendered with Apache ECharts, and automatically follow the light or dark theme of the page they are displayed in.
rtemis.draw is available from R-universe and is currently under review for CRAN.
Full documentation is available at docs.rtemis.org.
53.1 Setup
53.1.1 Installation
Until the package is released on CRAN, install it from R-universe:
53.1.2 Libraries
Load libraries we will be using in this chapter:
library(rtemis.draw)
library(data.table)
options(datatable.print.class = TRUE)53.1.3 Synthetic Data
We use the same synthetic dataset as in the previous two chapters:
set.seed(2022)
dt <- data.table(
PID = sample(8001:9000, size = 100),
Age = rnorm(100, mean = 33, sd = 8),
Weight = rnorm(100, mean = 70, sd = 9),
SysBP = rnorm(100, mean = 110, sd = 6),
DiaBP = rnorm(100, mean = 80, sd = 6),
Sex = factor(sample(c("Female", "Male"), size = 100, replace = TRUE))
)
dt[, SysBP := SysBP + 0.5 * Age]
dt[Sex == "Male", Weight := Weight + rnorm(.N, mean = 16, sd = 1.5)]
dt[Sex == "Male", Age := Age + rnorm(.N, mean = 6, sd = 1.8)]Define a color palette palette. Unlike in the previous chapters, we do not need a separate semi-transparent version of the palette: functions that fill areas, like draw_histogram() and draw_density(), have a fill_alpha argument, which is automatically set depending on whether there are multiple groups or not, but can be overridden if desired.
palette <- c("#43A4AC", "#FA9860")All draw_*() functions share a common set of arguments: title, xlab, ylab, theme, width, height, palette, and filename. If palette is not specified, the rtemis palette is used.
53.2 Box plot
draw_boxplot() accepts a numeric vector, a list of numeric vectors, or a data frame of numeric columns, and draws one box per element or column. Unlike ggplot2, there is no need to convert to long format first. Unlike plotly, there is no need to add each box one at a time.
draw_boxplot(dt[, .(SysBP, DiaBP)])Use palette to specify the colors and labels to specify the box labels:
draw_boxplot(
dt[, .(SysBP, DiaBP)],
palette = palette,
labels = c("Systolic BP", "Diastolic BP")
)53.2.1 Grouped boxplot
Use group to split a vector by a factor:
draw_boxplot(dt[, Age], group = dt[, Sex], palette = palette[2:1])53.3 Histogram
draw_histogram(dt[, Age], palette = palette[1])We can specify the number of bins with breaks, and axis labels with xlab and ylab:
draw_histogram(
dt[, Age],
breaks = 24,
palette = palette[1],
xlab = "Age (years)"
)53.3.1 Grouped Histogram
draw_histogram(
dt[, Age],
group = dt[, Sex],
palette = palette[2:1]
)bar_mode controls the placement of the groups’ bars within each bin: "group" (the default) places them side by side, whereas "stack" stacks them vertically.
draw_histogram(
dt[, Age],
group = dt[, Sex],
breaks = 24,
bar_mode = "stack",
palette = palette[2:1]
)53.4 Density plot
draw_density() estimates the density internally with stats::density(), so there is no need to compute and pass the coordinates ourselves as with plotly.
draw_density(dt[, Age], palette = palette[1])53.4.1 Grouped density plot
draw_density(dt[, Age], group = dt[, Sex], palette = palette[2:1])53.5 Barplot
schools <- data.frame(UCSF = 4, Stanford = 7, Penn = 12)draw_bar() takes the category labels as x and the bar heights as y. No data frame or explicit factor is needed; the bars appear in the order provided:
draw_bar(
names(schools),
as.numeric(schools[1, ]),
palette = palette[1],
ylab = "N schools"
)53.6 Scatterplot
draw_scatter(dt[, Age], dt[, SysBP], xlab = "Age", ylab = "SysBP", palette = palette[1])Pass fit = "gam" (or "glm") to overlay a fitted line and 95% confidence band:
draw_scatter(
dt[, Age], dt[, SysBP],
fit = "gam",
xlab = "Age", ylab = "SysBP",
palette = palette[1]
)53.6.1 Grouped Scatterplot
draw_scatter(
dt[, Age], dt[, SysBP],
group = dt[, Sex],
xlab = "Age", ylab = "SysBP",
palette = palette[2:1]
)With a grouping variable, fit draws a separate fitted line for each group:
draw_scatter(
dt[, Age], dt[, SysBP],
group = dt[, Sex],
fit = "gam",
xlab = "Age", ylab = "SysBP",
palette = palette[2:1]
)53.7 Themes
Charts follow the page theme (light or dark) by default. To force a theme, pass one to the theme argument:
draw_scatter(dt[, Age], dt[, SysBP], theme = theme_dark())53.8 Save plot to file
We’ll use the grouped boxplot example from above to show how to save a plot to file. Pass an .svg filename to any draw_*() function, or call save_drawing() on an existing widget. SVG export requires Node.js.
p <- draw_boxplot(dt[, Age], group = dt[, Sex], palette = palette[2:1])
save_drawing(p, "Age_by_Sex_rtemis.draw.svg", width = 550, height = 550)or directly:
draw_boxplot(
dt[, Age], group = dt[, Sex], palette = palette[2:1],
filename = "Age_by_Sex_rtemis.draw.svg"
)53.9 See also
- Base R Graphics (Chapter 49)
- Introduction to ggplot2 (Chapter 51)
- Introduction to plotly (Chapter 52)