dplot3_x(iris$Sepal.Length)
5 Interactive Graphics
Interactive graphics offer a flexible and dynamic way to communicate information, great for websites / web applications and live demonstrations. rtemis uses the powerful plotly open source graphing library and other libraries built on top of that.
While viewing these graphs, try using the mouse to hover over and click to interact with the graphic elements - especially with the 3D plots.
5.1 Density and Histograms
To plot multiple traces, you can either pass a list, or define groups by passing a factor to the group
argument. By default, mode = "overlap"
, which draws traces in the same plot.
dplot3_x(iris$Sepal.Length, group = iris$Species)
options(rt.theme = "darkgrayigrid") # can also pass theme = "darkgrayigrid" to dplot3_x
dplot3_x(iris)
Note that non-numeric columns are automatically omitted. You can set mode = "ridge"
to create a multiplot:
dplot3_x(iris, mode = "ridge")
By default, “ridge” mode will order plot order by variable mean. This can be changed using the ridge.order.on.mean
when you want to maintain group ordering - for example, if groups represent temporal information.
<- list(mango = rnorm(200, 7, 1),
xl banana = rnorm(200,10, .8),
tangerine = rnorm(400, 0, 2),
sugar = rnorm(500, 3, 1.5))
dplot3_x(xl)
dplot3_x(xl, mode = 'ridge')
dplot3_x(xl, mode = 'ridge', ridge.order.on.mean = FALSE)
5.2 Scatter plots
Here we are going to look at the static mplot3_xy
and mplot3_xym
, and the interactive dplot3_xy
.
Some synthetic data:
set.seed(2019)
<- rnorm(200)
x <- x^2 + rnorm(200, 2, 2)
square <- x^3 + rnorm(200, 3, 2) cube
dplot3_xy(x, cube, fit = "gam", se.fit = TRUE)
Lists (and therefore data.frames) are also supported here:
dplot3_xy(x, list(Square = square, Cube = cube),
fit = "gam", se.fit = TRUE,
theme = "darkgrayigrid")
5.2.1 Fit custom functions
dplot3_xy
includes a formula argument as an alternative to fit. This allows the user to define the formula of the fitting function, if that is known. As an example, let’s look at power curves. Power curves can help us model a number of important relationships that occur in nature. Let’s see how we can plot these in rtemis.
5.2.1.1 y = b * m ^ x
First, we create some synthetic data:
= 8102
set.seed <- rnorm(200)
x <- .8 * 2.7 ^ x
y.true <- y.true + .9 * rnorm(200) y
Just like with the static variant, mplot3_xy
, there are two ways to add a fit line in dplot3_xy
:
- The
fit
argument, e.g.fit = 'glm'
- The
formula
argument, e.g.formula = y ~ a * x + b
dplot3_xy(x, y, fit = 'gam')
dplot3_xy(x, y, formula = y ~ b * m ^ x)
5.2.2 Scatterplot + Cluster
We already saw we can use any learner to draw a fit line in a scatter plot. You can similarly use any clutering algorithm to cluster the data and color them by cluster membership. Learn more about [Clustering].
dplot3_xy(iris$Sepal.Length, iris$Petal.Length,
cluster = "hopach",
fit = "gam", se.fit = TRUE)
5.3 3D Scatter plots
The function for 3D scatterplots is dplot3_xyz
. You can specify x
, y
, and z
, or, pass a data.frame with at least 3 columns.
dplot3_xyz(iris, group = iris$Species)
5.3.1 Glass-cut plots
We can plot fitted surfaces using the fit
argument. The dependent variable is z
, i.e. we fit a model of the type z ~ x + y
.
Use the mouse to rotate the plot:
set.seed(2019)
<- rnorm(500)
x1 <- rnorm(500)
x2 <- x1^2 + x2^3 + 3 + rnorm(500) * 3
y dplot3_xyz(x1, x2, y, fit = "gam")
With groups:
dplot3_xyz(iris, fit = "glm", group = iris$Species)
dplot3_xyz(iris, fit = "gam", group = iris$Species)
dplot3_xyz(iris, fit = "cart", group = iris$Species)
5.4 Heatmaps
<- rnormmat(20, 20, seed = 2018)
x <- cor(x) x.cor
dplot3_heatmap(x.cor)
5.5 Barplots
dplot3_bar(VADeaths)
Note that for the time being, mplot3_bar
and dplot3_bar
treat columns and rows the opposite way, to conform with default graphics::barplot
and plotly behavior, respectively. This may or may not change, but the transpose function t()
is your friend.
5.6 Boxplots
Some synthetic data:
set.seed(1999)
<- list(mango = rnorm(200, 1, 1),
x banana = rpois(500, sample(c(0, 1, 2), 500, T)),
tangerine = rbinom(500, 1, .3),
sugar = rgamma(400, shape = 1))
dplot3_box(x)
5.7 Violin Plots
Violin plots are extended boxplots that visualize the actual variable distribution as density plots around the standard boxplot.
dplot3_box(x, type = "violin")
5.8 Pie Charts
Some real population data
<- structure(list(Continent = structure(c(2L, 1L, 3L, 6L, 4L, 5L),
x .Label = c("Africa", "Asia",
"Europe", "North America",
"Oceania", "South America"), class = "factor"),
Population = c(4601371198, 1308064195, 747182751,
427199446, 366600964, 42128035)),
class = "data.frame",
row.names = c(NA, -6L))
dplot3_pie(x)