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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
context("Guides")
skip_on_cran() # This test suite is long-running (on cran) and is skipped
test_that("colourbar trains without labels", {
g <- guide_colorbar()
sc <- scale_colour_continuous(limits = c(0, 4), labels = NULL)
out <- guide_train(g, sc)
expect_equal(names(out$key), c("colour", ".value"))
})
test_that("Colorbar respects show.legend in layer", {
df <- data_frame(x = 1:3, y = 1)
p <- ggplot(df, aes(x = x, y = y, color = x)) +
geom_point(size = 20, shape = 21, show.legend = FALSE)
expect_false("guide-box" %in% ggplotGrob(p)$layout$name)
p <- ggplot(df, aes(x = x, y = y, color = x)) +
geom_point(size = 20, shape = 21, show.legend = TRUE)
expect_true("guide-box" %in% ggplotGrob(p)$layout$name)
})
test_that("show.legend handles named vectors", {
n_legends <- function(p) {
g <- ggplotGrob(p)
gb <- which(g$layout$name == "guide-box")
if (length(gb) > 0) {
n <- length(g$grobs[[gb]]) - 1
} else {
n <- 0
}
n
}
df <- data_frame(x = 1:3, y = 20:22)
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20)
expect_equal(n_legends(p), 2)
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20, show.legend = c(color = FALSE))
expect_equal(n_legends(p), 1)
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20, show.legend = c(color = FALSE, shape = FALSE))
expect_equal(n_legends(p), 0)
# c.f.https://github.com/tidyverse/ggplot2/issues/3461
p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) +
geom_point(size = 20, show.legend = c(shape = FALSE, color = TRUE))
expect_equal(n_legends(p), 1)
})
test_that("axis_label_overlap_priority always returns the correct number of elements", {
expect_identical(axis_label_priority(0), numeric(0))
expect_setequal(axis_label_priority(1), seq_len(1))
expect_setequal(axis_label_priority(5), seq_len(5))
expect_setequal(axis_label_priority(10), seq_len(10))
expect_setequal(axis_label_priority(100), seq_len(100))
})
test_that("axis_label_element_overrides errors when angles are outside the range [0, 90]", {
expect_is(axis_label_element_overrides("bottom", 0), "element")
expect_error(axis_label_element_overrides("bottom", 91), "`angle` must")
expect_error(axis_label_element_overrides("bottom", -91), "`angle` must")
})
test_that("a warning is generated when guides are drawn at a location that doesn't make sense", {
plot <- ggplot(mpg, aes(class, hwy)) +
geom_point() +
scale_y_continuous(guide = guide_axis(position = "top"))
built <- expect_silent(ggplot_build(plot))
expect_warning(ggplot_gtable(built), "Position guide is perpendicular")
})
test_that("a warning is generated when more than one position guide is drawn at a location", {
plot <- ggplot(mpg, aes(class, hwy)) +
geom_point() +
guides(
y = guide_axis(position = "left"),
y.sec = guide_axis(position = "left")
)
built <- expect_silent(ggplot_build(plot))
expect_warning(ggplot_gtable(built), "Discarding guide")
})
test_that("a warning is not generated when properly changing the position of a guide_axis()", {
plot <- ggplot(mpg, aes(class, hwy)) +
geom_point() +
guides(
y = guide_axis(position = "right")
)
built <- expect_silent(ggplot_build(plot))
expect_silent(ggplot_gtable(built))
})
test_that("guide_none() can be used in non-position scales", {
p <- ggplot(mpg, aes(cty, hwy, colour = class)) +
geom_point() +
scale_color_discrete(guide = guide_none())
built <- ggplot_build(p)
plot <- built$plot
guides <- build_guides(
plot$scales,
plot$layers,
plot$mapping,
"right",
theme_gray(),
plot$guides,
plot$labels
)
expect_identical(guides, zeroGrob())
})
test_that("Using non-position guides for position scales results in an informative error", {
p <- ggplot(mpg, aes(cty, hwy)) +
geom_point() +
scale_x_continuous(guide = guide_legend())
built <- ggplot_build(p)
expect_error(ggplot_gtable(built), "does not implement guide_transform()")
})
test_that("guide merging for guide_legend() works as expected", {
merge_test_guides <- function(scale1, scale2) {
scale1$guide <- guide_legend(direction = "vertical")
scale2$guide <- guide_legend(direction = "vertical")
scales <- scales_list()
scales$add(scale1)
scales$add(scale2)
guide_list <- guides_train(scales, theme = theme_gray(), labels = labs(), guides = guides())
guides_merge(guide_list)
}
different_limits <- merge_test_guides(
scale_colour_discrete(limits = c("a", "b", "c", "d")),
scale_linetype_discrete(limits = c("a", "b", "c"))
)
expect_length(different_limits, 2)
same_limits <- merge_test_guides(
scale_colour_discrete(limits = c("a", "b", "c")),
scale_linetype_discrete(limits = c("a", "b", "c"))
)
expect_length(same_limits, 1)
expect_equal(same_limits[[1]]$key$.label, c("a", "b", "c"))
same_labels_different_limits <- merge_test_guides(
scale_colour_discrete(limits = c("a", "b", "c")),
scale_linetype_discrete(limits = c("one", "two", "three"), labels = c("a", "b", "c"))
)
expect_length(same_labels_different_limits, 1)
expect_equal(same_labels_different_limits[[1]]$key$.label, c("a", "b", "c"))
same_labels_different_scale <- merge_test_guides(
scale_colour_continuous(limits = c(0, 4), breaks = 1:3, labels = c("a", "b", "c")),
scale_linetype_discrete(limits = c("a", "b", "c"))
)
expect_length(same_labels_different_scale, 1)
expect_equal(same_labels_different_scale[[1]]$key$.label, c("a", "b", "c"))
repeated_identical_labels <- merge_test_guides(
scale_colour_discrete(limits = c("one", "two", "three"), labels = c("label1", "label1", "label2")),
scale_linetype_discrete(limits = c("1", "2", "3"), labels = c("label1", "label1", "label2"))
)
expect_length(repeated_identical_labels, 1)
expect_equal(repeated_identical_labels[[1]]$key$.label, c("label1", "label1", "label2"))
})
# Visual tests ------------------------------------------------------------
test_that("axis guides are drawn correctly", {
theme_test_axis <- theme_test() + theme(axis.line = element_line(size = 0.5))
test_draw_axis <- function(n_breaks = 3,
break_positions = seq_len(n_breaks) / (n_breaks + 1),
labels = as.character,
positions = c("top", "right", "bottom", "left"),
theme = theme_test_axis,
...) {
break_labels <- labels(seq_along(break_positions))
# create the axes
axes <- lapply(positions, function(position) {
draw_axis(break_positions, break_labels, axis_position = position, theme = theme, ...)
})
axes_grob <- gTree(children = do.call(gList, axes))
# arrange them so there's some padding on each side
gt <- gtable(
widths = unit(c(0.05, 0.9, 0.05), "npc"),
heights = unit(c(0.05, 0.9, 0.05), "npc")
)
gt <- gtable_add_grob(gt, list(axes_grob), 2, 2, clip = "off")
plot(gt)
}
# basic
expect_doppelganger("axis guides basic", function() test_draw_axis())
expect_doppelganger("axis guides, zero breaks", function() test_draw_axis(n_breaks = 0))
# overlapping text
expect_doppelganger(
"axis guides, check overlap",
function() test_draw_axis(20, labels = function(b) comma(b * 1e9), check.overlap = TRUE)
)
# rotated text
expect_doppelganger(
"axis guides, zero rotation",
function() test_draw_axis(10, labels = function(b) comma(b * 1e3), angle = 0)
)
expect_doppelganger(
"axis guides, positive rotation",
function() test_draw_axis(10, labels = function(b) comma(b * 1e3), angle = 45)
)
expect_doppelganger(
"axis guides, negative rotation",
function() test_draw_axis(10, labels = function(b) comma(b * 1e3), angle = -45)
)
expect_doppelganger(
"axis guides, vertical rotation",
function() test_draw_axis(10, labels = function(b) comma(b * 1e3), angle = 90)
)
expect_doppelganger(
"axis guides, vertical negative rotation",
function() test_draw_axis(10, labels = function(b) comma(b * 1e3), angle = -90)
)
# dodged text
expect_doppelganger(
"axis guides, text dodged into rows/cols",
function() test_draw_axis(10, labels = function(b) comma(b * 1e9), n.dodge = 2)
)
})
test_that("axis guides are drawn correctly in plots", {
expect_doppelganger("align facet labels, facets horizontal",
qplot(hwy, reorder(model, hwy), data = mpg) +
facet_grid(manufacturer ~ ., scales = "free", space = "free") +
theme_test() +
theme(strip.text.y = element_text(angle = 0))
)
expect_doppelganger("align facet labels, facets vertical",
qplot(reorder(model, hwy), hwy, data = mpg) +
facet_grid(. ~ manufacturer, scales = "free", space = "free") +
theme_test() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
)
expect_doppelganger("thick axis lines",
qplot(wt, mpg, data = mtcars) +
theme_test() +
theme(axis.line = element_line(size = 5, lineend = "square"))
)
})
test_that("axis guides can be customized", {
plot <- ggplot(mpg, aes(class, hwy)) +
geom_point() +
scale_y_continuous(
sec.axis = dup_axis(guide = guide_axis(n.dodge = 2)),
guide = guide_axis(n.dodge = 2)
) +
scale_x_discrete(guide = guide_axis(n.dodge = 2))
expect_doppelganger("guide_axis() customization", plot)
})
test_that("guides can be specified in guides()", {
plot <- ggplot(mpg, aes(class, hwy)) +
geom_point() +
guides(
x = guide_axis(n.dodge = 2),
y = guide_axis(n.dodge = 2),
x.sec = guide_axis(n.dodge = 2),
y.sec = guide_axis(n.dodge = 2)
)
expect_doppelganger("guides specified in guides()", plot)
})
test_that("guides have the final say in x and y", {
df <- data_frame(x = 1, y = 1)
plot <- ggplot(df, aes(x, y)) +
geom_point() +
guides(
x = guide_none(title = "x (primary)"),
y = guide_none(title = "y (primary)"),
x.sec = guide_none(title = "x (secondary)"),
y.sec = guide_none(title = "y (secondary)")
)
expect_doppelganger("position guide titles", plot)
})
test_that("guides are positioned correctly", {
df <- data_frame(x = 1, y = 1, z = factor("a"))
p1 <- ggplot(df, aes(x, y, colour = z)) +
geom_point() +
labs(title = "title of plot") +
theme_test() +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.background = element_rect(fill = "grey90"),
legend.key = element_rect(fill = "grey90")
) +
scale_x_continuous(breaks = 1, labels = "very long axis label") +
scale_y_continuous(breaks = 1, labels = "very long axis label")
expect_doppelganger("legend on left",
p1 + theme(legend.position = "left")
)
expect_doppelganger("legend on bottom",
p1 + theme(legend.position = "bottom")
)
expect_doppelganger("legend on right",
p1 + theme(legend.position = "right")
)
expect_doppelganger("legend on top",
p1 + theme(legend.position = "top")
)
expect_doppelganger("facet_grid, legend on left",
p1 + facet_grid(x~y) + theme(legend.position = "left")
)
expect_doppelganger("facet_grid, legend on bottom",
p1 + facet_grid(x~y) + theme(legend.position = "bottom")
)
expect_doppelganger("facet_grid, legend on right",
p1 + facet_grid(x~y) + theme(legend.position = "right")
)
expect_doppelganger("facet_grid, legend on top",
p1 + facet_grid(x~y) + theme(legend.position = "top")
)
expect_doppelganger("facet_wrap, legend on left",
p1 + facet_wrap(~ x) + theme(legend.position = "left")
)
expect_doppelganger("facet_wrap, legend on bottom",
p1 + facet_wrap(~ x) + theme(legend.position = "bottom")
)
expect_doppelganger("facet_wrap, legend on right",
p1 + facet_wrap(~ x) + theme(legend.position = "right")
)
expect_doppelganger("facet_wrap, legend on top",
p1 + facet_wrap(~ x) + theme(legend.position = "top")
)
# padding
dat <- data_frame(x = LETTERS[1:3], y = 1)
p2 <- ggplot(dat, aes(x, y, fill = x, colour = 1:3)) +
geom_bar(stat = "identity") +
guides(color = "colorbar") +
theme_test() +
theme(legend.background = element_rect(colour = "black"))
expect_doppelganger("padding in legend box", p2)
# Placement of legend inside
expect_doppelganger("legend inside plot, centered",
p2 + theme(legend.position = c(.5, .5))
)
expect_doppelganger("legend inside plot, bottom left",
p2 + theme(legend.justification = c(0,0), legend.position = c(0,0))
)
expect_doppelganger("legend inside plot, top right",
p2 + theme(legend.justification = c(1,1), legend.position = c(1,1))
)
expect_doppelganger("legend inside plot, bottom left of legend at center",
p2 + theme(legend.justification = c(0,0), legend.position = c(.5,.5))
)
})
test_that("guides title and text are positioned correctly", {
df <- data_frame(x = 1:3, y = 1:3)
p <- ggplot(df, aes(x, y, color = factor(x), fill = y)) +
geom_point(shape = 21) +
# setting the order explicitly removes the risk for failed doppelgangers
# due to legends switching order
guides(color = guide_legend(order = 2),
fill = guide_colorbar(order = 1)) +
theme_test()
expect_doppelganger("multi-line guide title works",
p +
scale_color_discrete(name = "the\ndiscrete\ncolorscale") +
scale_fill_continuous(name = "the\ncontinuous\ncolorscale")
)
expect_doppelganger("vertical gap of 1cm between guide title and guide",
p + theme(legend.spacing.y = grid::unit(1, "cm"))
)
expect_doppelganger("horizontal gap of 1cm between guide and guide text",
p + theme(legend.spacing.x = grid::unit(1, "cm"))
)
# now test label positioning, alignment, etc
df <- data_frame(x = c(1, 10, 100))
p <- ggplot(df, aes(x, x, color = x, size = x)) +
geom_point() +
# setting the order explicitly removes the risk for failed doppelgangers
# due to legends switching order
guides(shape = guide_legend(order = 1),
color = guide_colorbar(order = 2)) +
theme_test()
expect_doppelganger("guide title and text positioning and alignment via themes",
p + theme(
legend.title = element_text(hjust = 0.5, margin = margin(t = 30)),
legend.text = element_text(hjust = 1, margin = margin(l = 5, t = 10, b = 10))
)
)
# title and label rotation
df <- data_frame(x = c(5, 10, 15))
p <- ggplot(df, aes(x, x, color = x, fill = 15 - x)) +
geom_point(shape = 21, size = 5, stroke = 3) +
scale_colour_continuous(
name = "value",
guide = guide_colorbar(
title.theme = element_text(size = 11, angle = 0, hjust = 0.5, vjust = 1),
label.theme = element_text(size = 0.8*11, angle = 270, hjust = 0.5, vjust = 1),
order = 2 # set guide order to keep visual test stable
)
) +
scale_fill_continuous(
breaks = c(5, 10, 15),
limits = c(5, 15),
labels = paste("long", c(5, 10, 15)),
name = "fill value",
guide = guide_legend(
direction = "horizontal",
title.position = "top",
label.position = "bottom",
title.theme = element_text(size = 11, angle = 180, hjust = 0, vjust = 1),
label.theme = element_text(size = 0.8*11, angle = 90, hjust = 1, vjust = 0.5),
order = 1
)
)
expect_doppelganger("rotated guide titles and labels", p )
})
test_that("colorbar can be styled", {
df <- data_frame(x = c(0, 1, 2))
p <- ggplot(df, aes(x, x, color = x)) + geom_point()
expect_doppelganger("white-to-red gradient colorbar, white tick marks, no frame",
p + scale_color_gradient(low = 'white', high = 'red')
)
expect_doppelganger("white-to-red gradient colorbar, thick black tick marks, green frame",
p + scale_color_gradient(
low = 'white', high = 'red',
guide = guide_colorbar(
frame.colour = "green",
frame.linewidth = 1.5,
ticks.colour = "black",
ticks.linewidth = 2.5
)
)
)
})
test_that("guides can handle multiple aesthetics for one scale", {
df <- data_frame(x = c(1, 2, 3),
y = c(6, 5, 7))
p <- ggplot(df, aes(x, y, color = x, fill = y)) +
geom_point(shape = 21, size = 3, stroke = 2) +
scale_colour_viridis_c(
name = "value",
option = "B", aesthetics = c("colour", "fill")
)
expect_doppelganger("one combined colorbar for colour and fill aesthetics", p)
})
test_that("bin guide can be styled correctly", {
df <- data_frame(x = c(1, 2, 3),
y = c(6, 5, 7))
p <- ggplot(df, aes(x, y, size = x)) +
geom_point() +
scale_size_binned()
expect_doppelganger("guide_bins looks as it should", p)
expect_doppelganger("guide_bins can show limits",
p + guides(size = guide_bins(show.limits = TRUE))
)
expect_doppelganger("guide_bins can show arrows",
p + guides(size = guide_bins(axis.arrow = arrow(length = unit(1.5, "mm"), ends = "both")))
)
expect_doppelganger("guide_bins can remove axis",
p + guides(size = guide_bins(axis = FALSE))
)
expect_doppelganger("guide_bins work horizontally",
p + guides(size = guide_bins(direction = "horizontal"))
)
})
test_that("coloursteps guide can be styled correctly", {
df <- data_frame(x = c(1, 2, 4),
y = c(6, 5, 7))
p <- ggplot(df, aes(x, y, colour = x)) +
geom_point() +
scale_colour_binned(breaks = c(1.5, 2, 3))
expect_doppelganger("guide_coloursteps looks as it should", p)
expect_doppelganger("guide_coloursteps can show limits",
p + guides(colour = guide_coloursteps(show.limits = TRUE))
)
expect_doppelganger("guide_coloursteps can have bins relative to binsize",
p + guides(colour = guide_coloursteps(even.steps = FALSE))
)
expect_doppelganger("guide_bins can show ticks",
p + guides(colour = guide_coloursteps(ticks = TRUE))
)
})
|