library(DImodels)
library(dplyr)
library(ggplot2)
## Load data
data(sim0)
### Show raw data as points in ternary
## `ternary_plot` shows contours by default, use `show = "points"` to show
## points across the ternary
ternary_plot(data = sim0, prop = c("p1", "p2", "p3"), show = "points")
## The points can also be coloured using an additional variable by
## specifying it in `col_var`
ternary_plot(data = sim0, prop = c("p1", "p2", "p3"),
col_var = "response", show = "points")
## Categorical variables can also be shown
## Also show axis guides using `show_axis_guides`
sim0$richness <- as.factor(sim0$richness)
ternary_plot(data = sim0, prop = c("p1", "p2", "p3"),
col_var = "richness", show = "points",
show_axis_guides = TRUE)
## Change colours by using `colours` argument
## and increase points size using `points_size`
ternary_plot(data = sim0, prop = c("p1", "p2", "p3"),
col_var = "richness", show = "points",
colours = c("tomato", "steelblue", "orange"),
points_size = 4)
### Show contours of response
## Fit model
mod <- lm(response ~ 0 + (p1 + p2 + p3)^2, data = sim0)
## Create a contour map of predicted response over the ternary surface
## Remember to specify prop with the same character values as the names
## of the variables in the model containing the prop.
plot_data <- ternary_data(resolution = 1, model = mod,
prop = c("p1", "p2", "p3"))
## Create a contour plot of response across the ternary space
ternary_plot(plot_data)
## Change colour scheme
cols <- hcl.colors(7) # because there are 7 contour levels by default
ternary_plot(plot_data, colours = cols)
# \donttest{
## Change number of contours using `nlevels`
## and set custom upper and lower limits for the scale
ternary_plot(plot_data, nlevels = 10, colours = hcl.colors(10),
lower_lim = 10, upper_lim = 35)
## Change ternary labels along with their font-size
ternary_plot(plot_data, tern_labels = c("Sp1", "Sp2", "Sp3"),
vertex_label_size = 6, axis_label_size = 5)
## Add additional variables and create a separate plot for each
sim0$treatment <- rep(c("A", "B", "C", "D"), each = 16)
new_mod <- update(mod, ~. + treatment, data = sim0)
tern_data <- ternary_data(resolution = 1, model = new_mod,
prop = c("p1", "p2", "p3"),
add_var = list("treatment" = c("A", "C")))
## Arrange plot in 2 columns
ternary_plot(data = tern_data, ncol = 2)
# }
Run the code above in your browser using DataLab