forked from plotly/plotly.R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-plotly.R
More file actions
90 lines (81 loc) · 3.84 KB
/
test-plotly.R
File metadata and controls
90 lines (81 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
context("plot_ly")
expect_traces <- function(p, n.traces, name){
stopifnot(is.numeric(n.traces))
L <- save_outputs(p, paste0("plotly-", name))
expect_equal(length(L$data), n.traces)
L
}
test_that("plot_ly() handles a simple scatterplot", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers")
l <- expect_traces(p, 1, "scatterplot")
expect_identical(l$data[[1]]$mode, "markers")
expect_identical(l$data[[1]]$x, iris$Sepal.Length)
expect_identical(l$data[[1]]$y, iris$Petal.Length)
expect_identical(l$layout$xaxis$title, "Sepal.Length")
expect_identical(l$layout$yaxis$title, "Petal.Length")
})
test_that("Using group argument creates multiple traces", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, group = Species)
l <- expect_traces(p, 3, "scatterplot-group")
expect_identical(l$layout$xaxis$title, "Sepal.Length")
expect_identical(l$layout$yaxis$title, "Petal.Length")
})
test_that("Mapping a variable to symbol works", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, symbol = Species)
l <- expect_traces(p, 3, "scatterplot-symbol")
markers <- lapply(l$data, "[[", "marker")
syms <- unlist(lapply(markers, "[[", "symbol"))
expect_identical(syms, c("dot", "cross", "diamond"))
})
test_that("Mapping a factor variable to color works", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length,
color = Species, mode = "markers")
l <- expect_traces(p, 3, "scatterplot-color-factor")
markers <- lapply(l$data, "[[", "marker")
cols <- unlist(lapply(markers, "[[", "color"))
expect_equal(length(cols), 3)
})
test_that("Custom RColorBrewer pallette works for factor variable", {
cols <- RColorBrewer::brewer.pal(9, "Set1")
# specifying a pallette set should "span the gamut"
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length,
color = Species, colors = "Set1", mode = "markers")
l <- expect_traces(p, 3, "scatterplot-color-factor-custom")
markers <- lapply(l$data, "[[", "marker")
colz <- unlist(lapply(markers, "[[", "color"))
expect_identical(sort(cols[c(1, 5, 9)]), sort(colz))
# providing vector of RGB codes should also work
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length,
color = Species, colors = cols[1:3], mode = "markers")
l <- expect_traces(p, 3, "scatterplot-color-factor-custom2")
markers <- lapply(l$data, "[[", "marker")
colz <- unlist(lapply(markers, "[[", "color"))
expect_identical(sort(cols[1:3]), sort(colz))
})
test_that("Mapping a numeric variable to color works", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length,
color = Petal.Width, mode = "markers")
l <- expect_traces(p, 1, "scatterplot-color-numeric")
marker <- l$data[[1]]$marker
expect_identical(marker$colorbar$title, "Petal.Width")
expect_identical(marker$color, iris$Petal.Width)
expect_true(all(0 <= marker$colorscale[,1] & marker$colorscale[,1] <= 1))
})
test_that("Custom RColorBrewer pallette works for numeric variable", {
p <- plot_ly(data = iris, x = Sepal.Length, y = Petal.Length,
color = Petal.Width, colors = "Greens", mode = "markers")
l <- expect_traces(p, 1, "scatterplot-color-numeric-custom")
marker <- l$data[[1]]$marker
expect_identical(marker$colorbar$title, "Petal.Width")
expect_identical(marker$color, iris$Petal.Width)
expect_true(all(0 <= marker$colorscale[,1] & marker$colorscale[,1] <= 1))
})
test_that("axis titles get attached to scene object for 3D plots", {
p <- plot_ly(iris, x = Petal.Length, y = Petal.Width, z = Sepal.Width,
type = "scatter3d", mode = "markers")
l <- expect_traces(p, 1, "scatterplot-scatter3d-axes")
scene <- l$layout$scene
expect_identical(scene$xaxis$title, "Petal.Length")
expect_identical(scene$yaxis$title, "Petal.Width")
expect_identical(scene$zaxis$title, "Sepal.Width")
})