tests/testthat/test_scatterplots.R

context("Scatter and bubble plots")

data <- data.frame(
  x = c(2, 3, 5, 5.5, 7, 9, 2.5, 1, 5, 5.3, 8.5, 6.6),
  value = c(5,3,2,6, 7, 3, 2, 1,7,8,3, 5),
  cat = c("mlem","mlem","mlem","mlem","mlem", "kwa","kwa","kwa", "moo","moo","moo","moo"),
  bubble =c (1,2,3,4,5,4,6,2,1,3, 3.5, 4.5 )
)

data2 <- data.frame(
  x = c(2, 3, 5, 5.5, 7, 9, 2.5, 1, 5, 5.3, 8.5, -15),
  value = c(5,3,2,6, 7, 3, 2, 1,7,8,3, -5),
  cat = c("mlem","mlem","mlem","mlem","mlem", "kwa","kwa","kwa", "moo","moo","moo","moo"),
  bubble =c (1,2,3,4,5,4,6,2,1,3, 3.5, 4.5 )
)

data3 <- data.frame(
  x = c(2, 3, 5, 5.5, 7, 9, 2.5, 1, 5, 5.3, 8.5, -15),
  value = c(5,3,2,6, 7, 3, 2, 1,7,8,3, -5),
  cat = c("mlem","mlem","mlem","mlem","mlem", "kwa","kwa","kwa", "moo","moo","moo","moo"),
  bubble =c (1,2,3,4,5,-4,6,-2,1,-3, 3.5, 4.5 )
)

data4 <- data.frame(
  x = c(2, 3, 5, 5.5, 7, 9, 2.5, 1, 5, 5.3, 8.5, 15),
  value = c(5,3,2,6, 7, 3, 2, 1,7,8,3, 5),
  cat = c("mlem","mlem","mlem","mlem","mlem", "kwa","kwa","kwa", "moo","moo","moo","moo"),
  bubble =c (1,2,3,40,5,4,6, 2,1, 3, 3.5, 4.5 )
)

test_that("Basic scatterplot works without errors", {
  expect_magick(
    scatter_plot(
      data,
      data$x,
      data$value,
      data$cat,
      2,
      1,
      c("time", "in s"),
      c("distance", "in km"),
      "Legenda",
      data$bubble
    ) %>% show()
  )
})

test_that("Scatterplot with negative x and y value works without errors", {
  expect_magick(
    scatter_plot(
      data2,
      data2$x,
      data2$value,
      data2$cat,
      2,
      1,
      c("time", "in s"),
      c("distance", "in km"),
      "Legenda",
      data2$bubble
    ) %>% show()
  )
})

test_that("Simple scatterplot", {
  expect_magick(
    scatter_plot(
      data2,
      data2$x,
      data2$value,
      data2$cat,
      2,
      1,
      c("time", "in s"),
      c("distance", "in km"),
      "Legenda"
    ) %>% show()
  )
})

test_that("Scatterplot with negative bubble value throws error", {
  expect_error(
    scatter_plot(
      data3,
      data3$x,
      data3$value,
      data3$cat,
      2,
      1,
      c("time", "in s"),
      c("distance", "in km"),
      "Legenda",
      data3$bubble
    ) %>% show()
  )
})

test_that("Scatterplot with large difference in bubble size works without errors",
          {
            expect_magick(
              scatter_plot(
                data4,
                data4$x,
                data4$value,
                data4$cat,
                2,
                1,
                c("time", "in s"),
                c("distance", "in km"),
                "Legenda",
                data4$bubble
              ) %>% show()
            )
          })

test_that('Scatterplots with column names as args are ok',{
  expect_magick(scatter_plot(mtcars, x = 'hp', y = 'qsec',
               legend_title = '',
               x_names = c('Horsepower', 'in hp'),
               y_names = c('1/4 mile time', 'in s')) %>% show())
})

Try the tidycharts package in your browser

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

tidycharts documentation built on Jan. 18, 2022, 5:07 p.m.