File: test-ggplot-labels.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download | duplicates (2)
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

test_that("ggtitle is translated correctly", {
  ggpenguin <- ggplot(palmerpenguins::penguins) +
    geom_point(aes(bill_length_mm, bill_depth_mm)) +
    ggtitle("My amazing plot!")
  info <- expect_doppelganger_built(ggpenguin, "labels-ggtitle")
  expect_identical(info$layout$title$text, "My amazing plot!")
})

test_that("ylab is translated correctly", {
  ggpenguin <- ggplot(palmerpenguins::penguins) +
    geom_point(aes(bill_length_mm, bill_depth_mm)) +
    ylab("bill depth")
  info <- expect_doppelganger_built(ggpenguin, "labels-ylab")
  labs <- c(info$layout$xaxis$title$text, info$layout$yaxis$title$text)
  expect_identical(labs, c("bill_length_mm", "bill depth"))
})

test_that("scale_x_continuous(name) is translated correctly", {
  ggpenguin <- ggplot(palmerpenguins::penguins) +
    geom_point(aes(bill_length_mm, bill_depth_mm)) +
    scale_x_continuous("bill length")
  info <- expect_doppelganger_built(ggpenguin, "labels-scale_x_continuous_name")
  labs <- c(info$layout$xaxis$title$text, info$layout$yaxis$title$text)
  expect_identical(labs, c("bill length", "bill_depth_mm"))
})

test_that("angled ticks are translated correctly", {
  ggpenguin <- ggplot(palmerpenguins::penguins) +
    geom_point(aes(bill_length_mm, bill_depth_mm)) +
    theme(axis.text.x = element_text(angle = 45))
  info <- expect_doppelganger_built(ggpenguin, "labels-angles")
  expect_identical(info$layout$xaxis$tickangle, -45)
})

test_that("xaxis/yaxis automargin defaults to TRUE", {
  p <- ggplot(palmerpenguins::penguins, aes(species)) + geom_bar() + coord_flip()
  l <- plotly_build(p)$x
  expect_true(l$layout$xaxis$automargin)
  expect_true(l$layout$yaxis$automargin)
})

test_that("factor labels work", {
  p <- ggplot(diamonds, aes(cut)) + 
    geom_bar() + 
    scale_x_discrete("Cut", labels=factor(letters[1:5]))
  b <- expect_doppelganger_built(p, "factor-labels")
})

test_that("empty labels work", {
  p <- ggplot(palmerpenguins::penguins, 
              aes(bill_length_mm, bill_depth_mm, color = species)) + 
    geom_point() + 
    labs(x = element_blank(), y = element_blank())
  b <- expect_doppelganger_built(p, "labs-element-blank")
})