In Getting Started with ggswim, we showcased how to add markers to a swimmer plot using aesthetic mapping. This is a common use case, but not always the most applicable one. That’s why ggswim also caters to fixed marker assignments.
In this case, fixed marker assignment lets users call
add_marker()
and pass a single marker at a time with a
manually defined color parameter instead of passing a mapped column of
values. Let’s see how this works by splitting up
infusion_events
into distinct data frames.
library(ggswim)
library(ggplot2)
initial_infusions <- infusion_events |>
dplyr::filter(infusion_type == "Initial Infusion")
initial_infusions |>
rmarkdown::paged_table()
reinfusions <- infusion_events |>
dplyr::filter(infusion_type == "Reinfusion")
reinfusions |>
rmarkdown::paged_table()
We’ll start by setting up the same swimmer plot we’ve made from
previous examples. Recall our use of new_scale_color()
and
sequencing of scale changes:
p <- patient_data |>
ggswim(
mapping = aes(
x = start_time, xend = end_time, y = pt_id,
color = disease_assessment
),
linewidth = 5
) +
scale_color_manual(
name = "Overall Disease Assessment",
values = c("#6394F3", "#F3C363", "#EB792F", "#d73a76", "#85a31e")
)
Now, building off of this, lets add our
initial_infusions
and reinfusions
markers as
separate calls to add_marker()
:
p +
# Set a new scale
new_scale_color() +
# New manual color markers for Infusions
add_marker(
data = initial_infusions,
aes(x = time_from_initial_infusion, y = pt_id, name = "Initial Infusion"),
color = "green", size = 5, fixed_marker_name = "Markers"
) +
add_marker(
data = reinfusions,
aes(x = time_from_initial_infusion + 2, y = pt_id, name = "Reinfusion"),
color = "red", size = 5, fixed_marker_name = "Markers"
) +
theme_ggswim()
Notice here how we specified a color
outside of
aes()
, along with a unique aes()
param:
name
. name
is required to show static layers
in the legend, and also for proper display in cases where a static
marker is the first one added. Additionally, we used the parameter
fixed_maker_name
to set the title of the markers in the
legend.
Notes
- It is not recommended to use
add_marker()
in ggplot layering chains combining both aesthetic mapping and fixed mapping techniques - When using fixed markers, only one
new_scale_color()
call is permitted. - When using fixed markers, only one unique
fixed_marker_name
is permitted.