tests/testthat/test-geom-pointless.R

test_that("geom_pointless accepts only 'first', 'last', 'minimum', 'maximum', and 'all'", {
  df1 <- data.frame(x = 1:3, y = 1:3)
  p <- ggplot(df1, aes(x, y)) +
    geom_pointless(location = c("foo", "bar"))
  expect_warning(print(p))
})

test_that("location = 'all' is equal to c('first', 'last', 'minimum', 'maximum')", {
  set.seed(42)
  df2 <- data.frame(x = 1:10, y = sample(1:10))
  p1 <- ggplot(df2, aes(x, y)) +
    geom_pointless(location = c("all"))
  p2 <- ggplot(df2, aes(x, y)) +
    geom_pointless(location = c("first", "last", "minimum", "maximum"))
  expect_equal(layer_data(p1), layer_data(p2))

  df3 <- data.frame(
    var1 = 1:2,
    var2 = 1:2
  )
  p <- ggplot(df3, aes(x = var1, y = var2))
  p1 <- p + geom_pointless(aes(color = after_stat(location)),
    location = c("first", "last", "minimum", "maximum")
  )
  p2 <- p + geom_pointless(aes(color = after_stat(location)),
    location = c("maximum", "minimum", "last", "first", "all")
  )
  expect_equal(layer_data(p1), layer_data(p2))
})

test_that("geom_pointless works in both directions", {
  df3 <- data.frame(
    x = c(1, 2, 3),
    y = c(1, 2, 1)
  )

  p <- ggplot(df3, aes(x, y)) +
    geom_line() +
    geom_pointless(location = "all")
  x <- layer_data(p)
  expect_false(x$flipped_aes[1])

  p <- ggplot(df3, aes(y, x)) +
    geom_line(orientation = "y") +
    geom_pointless(location = "all", orientation = "y")
  y <- layer_data(p)
  expect_true(y$flipped_aes[1])

  x$flipped_aes <- NULL
  y$flipped_aes <- NULL
  expect_identical(x, ggplot2::flip_data(y, TRUE)[names(x)])
})

test_that("readme example works", {
  cols <- c("#f4ae1b", "#d77e7b", "#a84dbd", "#311dfc")
  x <- seq(-pi, pi, length.out = 500)
  y <- outer(x, 1:5, function(x, y) sin(x * y))

  df1 <- data.frame(
    var1 = x,
    var2 = rowSums(y)
  )

  p <- ggplot(df1, aes(x = var1, y = var2)) +
    geom_line() +
    geom_pointless(aes(color = after_stat(location)),
      location = "all",
      size = 3
    ) +
    scale_color_manual(values = cols) +
    theme_minimal()
  vdiffr::expect_doppelganger("readme geom_pointless example", p)
})

Try the ggpointless package in your browser

Any scripts or data that you put into this service are public.

ggpointless documentation built on May 29, 2024, 7:16 a.m.