Skip to content

Commit

Permalink
Fix examples and checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh52 committed Sep 30, 2024
1 parent bfcd470 commit aa82f7f
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 303 deletions.
11 changes: 7 additions & 4 deletions R/geom_swim_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@
#' @inheritParams ggplot2::geom_text
#'
#' @examples
#' \dontrun{
#' all_events <- dplyr::bind_rows(
#' infusion_events,
#' end_study_events
#' )
#'
#' ggplot() +
#' geom_swim_lane(data = patient_data,
#' aes(x = start_time, xend = end_time, y = pt_id, colour = disease_assessment)) +
#' ggplot2::ggplot() +
#' geom_swim_lane(
#' data = patient_data,
#' aes(x = start_time, xend = end_time, y = pt_id, colour = disease_assessment)
#' ) +
#' geom_swim_marker(
#' data = all_events,
#' aes(x = time_from_initial_infusion, y = pt_id, marker = label),
#' size = 10
#' )
#'
#'}
#'
#' @export

Expand Down
35 changes: 16 additions & 19 deletions R/scale_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@
#' @inheritParams ggplot2::discrete_scale
#'
#' @examples
#'
#' \dontrun{
#' all_events <- dplyr::bind_rows(
#' infusion_events,
#' end_study_events
#' )
#'
#' ggplot() +
#' geom_swim_marker(
#' data = all_events,
#' aes(x = time_from_initial_infusion, y = pt_id, marker = label),
#' size = 5
#' ) +
#' scale_marker_discrete(glyphs = all_events$glyph,
#' colours = all_events$colour,
#' limits = all_events$label)
#' ggplot2::ggplot() +
#' geom_swim_marker(
#' data = all_events,
#' aes(x = time_from_initial_infusion, y = pt_id, marker = label),
#' size = 5
#' ) +
#' scale_marker_discrete(
#' glyphs = all_events$glyph,
#' colours = all_events$colour,
#' limits = all_events$label
#' )
#'}
#'
#' @export

scale_marker_discrete <- function(glyphs = NULL, colours = NULL, limits = NULL, ...) {
n_values <- max(length(glyphs), length(colours))

markers <- data.frame(
colours = rep(colours %||% .default_colours, length.out = n_values),
glyphs = rep(glyphs %||% .default_glyphs, length.out = n_values),
labels = limits
) |>
markers <- data.frame(glyphs = glyphs, colours = colours, labels = limits) |>
distinct()

palette <- pal_markers(
Expand All @@ -48,8 +45,8 @@ scale_marker_discrete <- function(glyphs = NULL, colours = NULL, limits = NULL,
)

discrete_scale("marker", rlang::missing_arg(),
palette = palette,
limits = markers$labels, ..., na.translate = FALSE
palette = palette,
limits = markers$labels, ..., na.translate = FALSE
)
}

Expand Down
51 changes: 25 additions & 26 deletions R/theme_ggswim.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
#'
#' @examples
#' p <- ggplot2::ggplot() +
#' geom_swim_lane(
#' data = patient_data,
#' mapping = aes(
#' x = start_time,
#' xend = end_time,
#' y = pt_id,
#' colour = disease_assessment
#' ),
#' linewidth = 3
#' )
#' geom_swim_lane(
#' data = patient_data,
#' mapping = aes(
#' x = start_time,
#' xend = end_time,
#' y = pt_id,
#' colour = disease_assessment
#' ),
#' linewidth = 3
#' )
#'
#' p +
#' theme_ggswim()
Expand All @@ -47,7 +47,7 @@ theme_ggswim <- function(base_size = 12, base_family = "") {

# Axis text
axis.text = element_text(size = 10, face = "bold", colour = "#0D2C54"),
axis.text.x = element_text(margin = margin(t =10)),
axis.text.x = element_text(margin = margin(t = 10)),
axis.text.y = element_text(margin = margin(r = 10)),

# Axis titles
Expand All @@ -56,7 +56,7 @@ theme_ggswim <- function(base_size = 12, base_family = "") {
axis.title.y = element_text(size = 14, face = "bold", margin = margin(r = 10)),

# Axis tick marks
axis.ticks = element_line(size = 0.75, colour = "steelblue"),
axis.ticks = element_line(linewidth = 0.75, colour = "steelblue"),
axis.ticks.length = unit(0.3, "cm"),

# Legend
Expand Down Expand Up @@ -85,37 +85,37 @@ theme_ggswim_dark <- function(base_size = 12, base_family = "") {
theme_minimal(base_size = base_size, base_family = base_family) +
theme(
# Background
plot.background = element_rect(fill = "#232234", color = NA), # Dark blue background
panel.background = element_rect(fill = "#232234", color = NA), # Dark blue panel background
plot.background = element_rect(fill = "#232234", color = NA),
panel.background = element_rect(fill = "#232234", color = NA),

# Title and subtitle
plot.title = element_text(size = 18, face = "bold", hjust = 0, margin = margin(b = 10), colour = "#FE9000"), # Orange title
plot.subtitle = element_text(size = 14, face = "bold", hjust = 0, margin = margin(b = 10), colour = "#D67A00"), # Light orange subtitle
plot.title = element_text(size = 18, face = "bold", hjust = 0, margin = margin(b = 10), colour = "#FE9000"),
plot.subtitle = element_text(size = 14, face = "bold", hjust = 0, margin = margin(b = 10), colour = "#D67A00"),

# Caption
plot.caption = element_text(size = 10, hjust = 1, colour = "#F0F0F0"), # Light caption text
plot.caption = element_text(size = 10, hjust = 1, colour = "#F0F0F0"),

# Axis text
axis.text = element_text(size = 10, face = "bold", colour = "#F0F0F0"), # Light gray axis text
axis.text.x = element_text(margin = margin(t = 10), colour = "#F0F0F0"), # Light gray axis text for x-axis
axis.text.y = element_text(margin = margin(r = 10), colour = "#F0F0F0"), # Light gray axis text for y-axis
axis.text = element_text(size = 10, face = "bold", colour = "#F0F0F0"),
axis.text.x = element_text(margin = margin(t = 10), colour = "#F0F0F0"),
axis.text.y = element_text(margin = margin(r = 10), colour = "#F0F0F0"),

# Axis titles
axis.title = element_text(size = 12, face = "bold", colour = "#FE9000"), # Orange axis titles
axis.title = element_text(size = 12, face = "bold", colour = "#FE9000"),
axis.title.x = element_text(size = 14, face = "bold", margin = margin(t = 10), colour = "#FE9000"),
axis.title.y = element_text(size = 14, face = "bold", margin = margin(r = 10), colour = "#FE9000"),

# Axis tick marks
axis.ticks = element_line(size = 0.75, colour = "#FE9000"), # Orange axis ticks
axis.ticks = element_line(linewidth = 0.75, colour = "#FE9000"),
axis.ticks.length = unit(0.3, "cm"),

# Legend
legend.text = element_text(size = 10, colour = "#F0F0F0"), # Light gray legend text
legend.title = element_text(size = 12, face = "bold", colour = "#FE9000"), # Orange legend title
legend.text = element_text(size = 10, colour = "#F0F0F0"),
legend.title = element_text(size = 12, face = "bold", colour = "#FE9000"),

# Axis line
axis.line = element_line(
color = "#FE9000", # Orange axis line
color = "#FE9000", # Orange axis line
linewidth = 1,
arrow = arrow(
type = "closed",
Expand All @@ -128,4 +128,3 @@ theme_ggswim_dark <- function(base_size = 12, base_family = "") {
panel.grid.minor = element_blank()
)
}

28 changes: 15 additions & 13 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ library(ggplot2)
# Construct arrow_data for arrow display later
arrow_data <- patient_data |>
dplyr::left_join(
end_study_events |>
dplyr::select(pt_id, label),
by = "pt_id"
) |>
dplyr::select(pt_id, end_time, label) |>
dplyr::filter(.by = pt_id, end_time == max(end_time)) |>
dplyr::filter(is.na(label)) |>
unique()
dplyr::left_join(
end_study_events |>
dplyr::select(pt_id, label),
by = "pt_id"
) |>
dplyr::select(pt_id, end_time, label) |>
dplyr::filter(.by = pt_id, end_time == max(end_time)) |>
dplyr::filter(is.na(label)) |>
unique()
p <- patient_data |>
ggplot() +
Expand Down Expand Up @@ -100,10 +100,12 @@ This looks OK as a default, but it's not quite as nice as we'd like it to be. Le

```{r, message = FALSE}
p <- p +
scale_marker_discrete(glyphs = all_events$glyph,
colours = all_events$colour,
limits = all_events$label,
name = "Study Events")
scale_marker_discrete(
glyphs = all_events$glyph,
colours = all_events$colour,
limits = all_events$label,
name = "Study Events"
)
p
```
Expand Down
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ library(ggplot2)

# Construct arrow_data for arrow display later
arrow_data <- patient_data |>
dplyr::left_join(
end_study_events |>
dplyr::select(pt_id, label),
by = "pt_id"
) |>
dplyr::select(pt_id, end_time, label) |>
dplyr::filter(.by = pt_id, end_time == max(end_time)) |>
dplyr::filter(is.na(label)) |>
unique()
dplyr::left_join(
end_study_events |>
dplyr::select(pt_id, label),
by = "pt_id"
) |>
dplyr::select(pt_id, end_time, label) |>
dplyr::filter(.by = pt_id, end_time == max(end_time)) |>
dplyr::filter(is.na(label)) |>
unique()

p <- patient_data |>
ggplot() +
Expand Down Expand Up @@ -111,10 +111,12 @@ like to use for the markers with ggswim’s `scale_marker_discrete()`.

``` r
p <- p +
scale_marker_discrete(glyphs = all_events$glyph,
colours = all_events$colour,
limits = all_events$label,
name = "Study Events")
scale_marker_discrete(
glyphs = all_events$glyph,
colours = all_events$colour,
limits = all_events$label,
name = "Study Events"
)

p
```
Expand Down
11 changes: 7 additions & 4 deletions man/geom_swim_marker.Rd

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

23 changes: 13 additions & 10 deletions man/scale_marker_discrete.Rd

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

20 changes: 10 additions & 10 deletions man/theme_ggswim.Rd

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

Loading

0 comments on commit aa82f7f

Please sign in to comment.