-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathtest-plotly-subplot.R
More file actions
173 lines (158 loc) · 6.46 KB
/
test-plotly-subplot.R
File metadata and controls
173 lines (158 loc) · 6.46 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
context("subplot")
expect_traces <- function(p, n.traces, name){
stopifnot(is.numeric(n.traces))
L <- save_outputs(p, paste0("plotly-subplot-", name))
expect_equivalent(length(L$data), n.traces)
L
}
test_that("simple subplot works", {
p1 <- plot_ly(x = c(1, 2))
p2 <- plot_ly(x = c(1, 2))
s <- expect_traces(subplot(p1, p2), 2, "simple")
expect_identical(s$data[[2]]$xaxis, s$layout[["yaxis2"]][["anchor"]])
expect_identical(s$data[[2]]$yaxis, s$layout[["xaxis2"]][["anchor"]])
doms <- lapply(s$layout[grepl("^xaxis", names(s$layout))], "[[", "domain")
expect_true(doms$xaxis[2] <= doms$xaxis2[1])
})
test_that("nrows argument works", {
p1 <- plot_ly(x = c(1, 2))
p2 <- plot_ly(x = c(1, 2))
s <- expect_traces(subplot(p1, p2, nrows = 2), 2, "simple2")
expect_identical(s$data[[2]]$xaxis, s$layout[["yaxis2"]][["anchor"]])
expect_identical(s$data[[2]]$yaxis, s$layout[["xaxis2"]][["anchor"]])
doms <- lapply(s$layout[grepl("^[x-y]axis", names(s$layout))], "[[", "domain")
expect_true(doms$yaxis[2] > doms$yaxis[1])
expect_true(doms$yaxis[1] > doms$yaxis2[2])
expect_true(doms$yaxis2[2] > doms$yaxis2[1])
})
test_that("group + [x/y]axis works", {
iris$id <- as.integer(iris$Species)
p <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, color = ~Species,
xaxis = ~paste0("x", id), mode = "markers")
s <- expect_traces(subplot(p, margin = 0.05), 3, "group")
ax <- s$layout[grepl("^[x-y]axis", names(s$layout))]
doms <- lapply(ax, "[[", "domain")
# make sure y domain is [0, 1] on every axis
ydom <- doms[grepl("^y", names(doms))]
expect_equivalent(sort(unique(unlist(ydom))), c(0, 1))
xdom <- doms[grepl("^x", names(doms))]
expect_true(all(1/3 > xdom[[1]] & xdom[[1]] >= 0))
expect_true(all(2/3 > xdom[[2]] & xdom[[2]] > 1/3))
expect_true(all(1 >= xdom[[3]] & xdom[[3]] > 2/3))
})
test_that("shareX produces one x-axis and a legend", {
s <- subplot(plot_ly(x = 1), plot_ly(x = 1), nrows = 2, shareX = TRUE)
l <- expect_traces(s, 2, "shareX")
expect_true(sum(grepl("^xaxis", names(l$layout))) == 1)
expect_true(l$data[[1]]$showlegend %||% TRUE)
expect_true(l$data[[2]]$showlegend %||% TRUE)
expect_true(l$layout$showlegend %||% TRUE)
})
test_that("shareY produces one y-axis", {
s <- subplot(plot_ly(x = 1), plot_ly(x = 1), shareY = TRUE)
l <- expect_traces(s, 2, "shareY")
expect_true(sum(grepl("^yaxis", names(l$layout))) == 1)
})
test_that("share both axes", {
s <- subplot(
plot_ly(x = 1), plot_ly(x = 1), plot_ly(x = 1), plot_ly(x = 1),
nrows = 2, shareX = TRUE, shareY = TRUE
)
l <- expect_traces(s, 4, "shareBoth")
expect_true(sum(grepl("^yaxis", names(l$layout))) == 2)
expect_true(sum(grepl("^xaxis", names(l$layout))) == 2)
})
# https://github.com/ropensci/plotly/issues/376
d <- data.frame(
x = rnorm(100),
y = rnorm(100)
)
hist_top <- ggplot(d) + geom_histogram(aes(x = x))
empty <- ggplot() + geom_blank()
scatter <- ggplot(d) + geom_point(aes(x = x, y = y))
hist_right <- ggplot(d) + geom_histogram(aes(x = y)) + coord_flip()
s <- subplot(
hist_top, empty, empty, scatter, empty, hist_right,
nrows = 2, widths = c(0.5, 0.3, 0.2), heights = c(0.4, 0.6),
margin = 0.005, shareX = TRUE, shareY = TRUE
)
test_that("Row/column height/width", {
l <- expect_traces(s, 3, "width-height")
expect_equivalent(diff(l$layout$xaxis$domain), 0.5 - 0.005)
expect_equivalent(diff(l$layout$xaxis2$domain), 0.3 - 0.005)
expect_equivalent(diff(l$layout$xaxis3$domain), 0.2 - 0.005)
expect_equivalent(diff(l$layout$yaxis$domain), 0.4 - 0.005)
expect_equivalent(diff(l$layout$yaxis2$domain), 0.6 - 0.005)
})
test_that("recursive subplots work", {
p1 <- plot_ly(economics, x = ~date, y = ~unemploy)
p2 <- plot_ly(economics, x = ~date, y = ~uempmed)
s1 <- subplot(p1, p1, shareY = TRUE)
s2 <- subplot(p2, p2, shareY = TRUE)
s <- subplot(s1, s2, nrows = 2, shareX = TRUE)
l <- expect_traces(s, 4, "recursive")
xaxes <- l$layout[grepl("^xaxis", names(l$layout))]
yaxes <- l$layout[grepl("^yaxis", names(l$layout))]
expect_true(length(xaxes) == 2)
expect_true(length(yaxes) == 2)
# both x-axes are anchored on the same y-axis
yanchor <- unique(unlist(lapply(xaxes, "[[", "anchor")))
expect_true(length(yanchor) == 1)
# both y-axes are anchored on the same x-axis
xanchor <- unique(unlist(lapply(yaxes, "[[", "anchor")))
expect_true(length(xanchor) == 1)
# x/y are anchored on the bottom/left
expect_true(l$layout[[sub("x", "xaxis", xanchor)]]$domain[1] == 0)
expect_true(l$layout[[sub("y", "yaxis", yanchor)]]$domain[1] == 0)
# every trace is anchored on a different x/y axis pair
xTraceAnchors <- sapply(l$data, "[[", "xaxis")
yTraceAnchors <- sapply(l$data, "[[", "yaxis")
expect_true(length(unique(paste(xTraceAnchors, yTraceAnchors))) == 4)
})
test_that("subplot accepts a list of plots", {
vars <- setdiff(names(economics), "date")
plots <- lapply(vars, function(var) {
plot_ly(x = economics$date, y = economics[[var]], name = var)
})
s <- subplot(plots, nrows = length(plots), shareX = TRUE, titleX = FALSE)
l <- expect_traces(s, 5, "plot-list")
xaxes <- l$layout[grepl("^xaxis", names(l$layout))]
yaxes <- l$layout[grepl("^yaxis", names(l$layout))]
expect_true(length(xaxes) == 1)
expect_true(length(yaxes) == 5)
# x-axis is anchored at the bottom
expect_true(l$layout[[sub("y", "yaxis", xaxes[[1]]$anchor)]]$domain[1] == 0)
})
test_that("ggplotly understands ggmatrix", {
L <- save_outputs(GGally::ggpairs(iris), "plotly-subplot-ggmatrix")
})
test_that("geo+cartesian behaves", {
# specify some map projection/options
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
lakecolor = toRGB('white')
)
# create a map of population density
density <- state.x77[, "Population"] / state.x77[, "Area"]
map <- plot_geo(
z = ~density, text = state.name,
locations = state.abb, locationmode = 'USA-states'
) %>% layout(geo = g)
# create a bunch of horizontal bar charts
vars <- colnames(state.x77)
barcharts <- lapply(vars, function(var) {
plot_ly(x = state.x77[, var], y = state.name, type = "bar",
orientation = "h", name = var) %>%
layout(showlegend = FALSE, hovermode = "y",
yaxis = list(showticklabels = FALSE))
})
s <- subplot(
subplot(barcharts, margin = 0.01), map,
nrows = 2, heights = c(0.3, 0.7)
)
l <- expect_traces(s, 9, "geo-cartesian")
geoDom <- l$layout[[grep("^geo", names(l$layout))]]$domain
expect_equivalent(geoDom$x, c(0, 1))
expect_equivalent(geoDom$y, c(0, 0.68))
})