Skip to content

Commit

Permalink
Improve scale tests, add gallery plots
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh52 committed Oct 1, 2024
1 parent aa82f7f commit 88410a2
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 62 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export("%+%")
export(.default_colours)
export(.default_glyphs)
export(.default_limits)
export(GeomSwimArrow)
export(GeomSwimLane)
export(GeomSwimMarker)
Expand Down
20 changes: 18 additions & 2 deletions R/scale_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@
#' @export

scale_marker_discrete <- function(glyphs = NULL, colours = NULL, limits = NULL, ...) {
markers <- data.frame(glyphs = glyphs, colours = colours, labels = limits) |>
distinct()
# Define max value lengths for core params
n_values <- max(c(length(glyphs), length(colours), length(limits)))

# If no params assigned (default when using geom_swim_marker), return empty
# Else, intelligently create equal length core vectors
if (n_values == 0) {
markers <- data.frame()
} else {
markers <- data.frame(glyphs = glyphs %||% .default_glyphs[0:n_values],
colours = colours %||% .default_colours[0:n_values],
labels = limits %||% .default_limits[0:n_values]) |>
distinct()
}

palette <- pal_markers(
glyphs = markers$glyphs,
Expand Down Expand Up @@ -84,6 +95,7 @@ pal_markers <- function(glyphs = NULL, colours = NULL, n_values = NULL) {
#' @examples
#' ggswim::.default_glyphs
#' ggswim::.default_colours
#' ggswim::.default_limits
#'
#' @export
# Set default glyphs and colours
Expand All @@ -92,3 +104,7 @@ pal_markers <- function(glyphs = NULL, colours = NULL, n_values = NULL) {
#' @rdname dot-default_glyphs
#' @export
.default_colours <- scales::brewer_pal(palette = "Set1")(9) # 9 is max for brewer

#' @rdname dot-default_glyphs
#' @export
.default_limits <- c("val1", "val2", "val3", "val4", "val5", "val6", "val7", "val8", "val9")
6 changes: 6 additions & 0 deletions man/dot-default_glyphs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 24 additions & 9 deletions tests/testthat/test-geom_swim_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,32 @@ test_that("all expected attributes exist in geom_swim_lane", {
expect_true(all(expected_attrs %in% attrs))
})

test_that("geom_swim_lane works when inheriting data and associated params", {
p <- patient_data |>
ggplot(
data = infusion_events,
mapping = aes(
x = time_from_initial_infusion, y = pt_id,
marker = label
)
) +
test_that("geom_swim_marker works when inheriting data and associated params", {
p <- ggplot(
data = infusion_events,
mapping = aes(
x = time_from_initial_infusion, y = pt_id,
marker = label
)
) +
geom_swim_marker()

skip_on_ci()
vdiffr::expect_doppelganger(
title = "geom_swim_marker works with inherited data and params",
fig = p
)
})

test_that("geom_swim_lane works when inheriting data and associated params", {
p <- ggplot(
data = patient_data,
mapping = aes(
x = start_time, xend = end_time, y = pt_id, colour = disease_assessment
)
) +
geom_swim_lane()

skip_on_ci()
vdiffr::expect_doppelganger(
title = "geom_swim_lane works with inherited data and params",
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-scale_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ test_that("scale_marker_discrete warns when more markers are requested than avai
expect_error(scale$palette(5), "Can't subset elements past the end.") |>
suppressWarnings()
})

test_that("scale_marker_discrete uses default glyphs when missing", {
colours <- c("red", "green", "blue")
limits <- c("A", "B", "C")

scale <- scale_marker_discrete(glyphs = NULL, colours = colours, limits = limits)

expect_equal(length(scale$palette(3)), 3)
expect_equal(vctrs::field(scale$palette(3), "glyphs"), .default_glyphs[1:3]) # First 3 default glyphs
})

test_that("scale_marker_discrete uses default colours when missing", {
glyphs <- c("●", "β– ", "β–²")
limits <- c("A", "B", "C")

scale <- scale_marker_discrete(glyphs = glyphs, colours = NULL, limits = limits)

expect_equal(length(scale$palette(3)), 3)
expect_equal(vctrs::field(scale$palette(3), "colour"), .default_colours[1:3]) # First 3 default glyphs
})
72 changes: 63 additions & 9 deletions vignettes/articles/gallery.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,70 @@ library(ggplot2)
library(dplyr)
```

To support some of these visuals, we'll set up some of the datasets similar to the README:
## Random Data Sets

```{r setup data}
In this example, we'll set up a random dataset for reproducibility by separately defining a dataframe for our lanes and our markers.

```{r}
set.seed(123)

Check warning on line 27 in vignettes/articles/gallery.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/articles/gallery.Rmd,line=27,col=14,[trailing_whitespace_linter] Trailing whitespace is superfluous.
lane_data <- tibble(
x = 0,
xend = sample(5:20, 30, replace = TRUE),
y = factor(rep(1:15, each = 2)),
colour = sample(c('red', 'blue', 'green', 'yellow', 'purple'), 30, replace = TRUE)

Check warning on line 32 in vignettes/articles/gallery.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/articles/gallery.Rmd,line=32,col=21,[quotes_linter] Only use double-quotes.

Check warning on line 32 in vignettes/articles/gallery.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/articles/gallery.Rmd,line=32,col=28,[quotes_linter] Only use double-quotes.

Check warning on line 32 in vignettes/articles/gallery.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/articles/gallery.Rmd,line=32,col=36,[quotes_linter] Only use double-quotes.

Check warning on line 32 in vignettes/articles/gallery.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=vignettes/articles/gallery.Rmd,line=32,col=45,[quotes_linter] Only use double-quotes.
)
set.seed(123)
marker_data <- tibble(
x = sample(5:20, 30, replace = TRUE),
y = factor(rep(1:15, each = 2)),
label = sample(c("A", "B", "C", "D", "E"), 30, replace = TRUE),
glyph = sample(c("😊", "πŸŽ‰", "βœ…", "πŸ’₯", "✨"), 30, replace = TRUE)
) |>
mutate(
glyph = dplyr::case_when(
label == "A" ~ "😊",
label == "B" ~ "πŸŽ‰",
label == "C" ~ "βœ…",
label == "D" ~ "πŸ’₯",
label == "E" ~ "✨",
.default = NA
)
)
lane_data |>
rmarkdown::paged_table()
marker_data |>
rmarkdown::paged_table()
```

And then we'll call those datasets into their appropriate swim and marker geom functions:

```{r}
ggplot() +
geom_swim_lane(
data = lane_data,
aes(x = x, xend = xend, y = y, colour = colour),
linewidth = 3
) +
geom_swim_marker(
data = marker_data,
aes(x = x, y = y, marker = label),
size = 8
) +
scale_colour_brewer(name = "Lanes", palette = "Set1") +
with(marker_data,
scale_marker_discrete(glyphs = glyph, limits = label, name = "Markers")) +
labs(title = "Sample Swimmer Plot",
x = "Time", y = "Record ID") +
theme_ggswim()
```

## Themeing with ggswim

Here are some example of the themeing functions available in ggswim using the dataset examples from the README:

```{r}
arrow_data <- patient_data |>
dplyr::left_join(
end_study_events |>
Expand All @@ -37,13 +98,7 @@ all_events <- dplyr::bind_rows(
infusion_events,
end_study_events
)
```

## Themeing with ggswim
Here are some example of the themeing functions available in ggswim using the dataset examples from the README:

```{r}
p <- patient_data |>
ggplot() +
geom_swim_lane(
Expand Down Expand Up @@ -91,4 +146,3 @@ p + theme_ggswim()
```{r}
p + theme_ggswim_dark()
```

0 comments on commit 88410a2

Please sign in to comment.