Documentation for ggsced
Amending Axis Contact using ggplot
Single-case design plotting considerations often frustrate even the most basic use of plotting software. In ggplot, like most other tools, it is generally the case the axes meet at the origin, assuming a common 0 point. A relevant example of this, using data from previously-published functional analysis, is provided below:
ggplot(data, aes(x = Session,
y = CTB,
shape = Condition,
fill = Condition)) +
geom_line() +
geom_point(size = 5) +
scale_shape_manual(values = c("Toy Play" = 16,
"Attention" = 22,
"Demand" = 24,
"Tangible" = 8)) +
scale_fill_manual(values = c("Toy Play" = "black",
"Attention" = "white",
"Demand" = "white",
"Tangible" = "black")) +
theme_classic() +
theme(
text = element_text(family = "Times New Roman", size = 14),
legend.position = "inside",
legend.position.inside = c(1, 1),
legend.justification.inside = c(1, 1)
)
Common Overrides for x- and y-axis settings
It is generally the case that users can customize the desired tick marks using various position scales (e.g., *scale*{x/y}***). The previous figure can be adapted to apply more clinically useful settings via the overrides below.
ggplot(data, aes(x = Session,
y = CTB,
shape = Condition,
fill = Condition)) +
geom_line() +
geom_point(size = 5) +
scale_shape_manual(values = c("Toy Play" = 16,
"Attention" = 22,
"Demand" = 24,
"Tangible" = 8)) +
scale_fill_manual(values = c("Toy Play" = "black",
"Attention" = "white",
"Demand" = "white",
"Tangible" = "black")) +
# Note: breaks = axis ticks, limits = custom min/max ticks
scale_x_continuous(breaks = 1:15,
limits = c(1, 15)) +
scale_y_continuous(breaks = c(0, 0.5, 1, 1.5),
limits = c(0, 1.5)) +
theme_classic() +
theme(
text = element_text(family = "Times New Roman", size = 14),
legend.position = "inside",
legend.position.inside = c(1, 1),
legend.justification.inside = c(1, 1)
)
The updated plot provide more meaningful ticks, which is critical for clear clinical interpretation, but the overall presentation of the axes would be considered counter to historical conventions (i.e., they should not extend beyond min and maximum ticks).
Updates to ggplot regarding Axis Guides
The developers of ggplot expanded the position scales in version 3.5.0 and provided additional options for updating the position scales. Fortunately, for users of single-case designs, this new functionality provides a means to accommodate some of the historical conventions in single-case design research.
Specifically, the guide field in the position scale can take guide_axis function with specialized overrides to expand the default behavior. Specifically, the default behavior for ‘capping’ axis lines are to not cap at all (i.e., cap = “none”). In practice, this means that the axis line extends fully throughout the plot panel. The expanded settings not permit the capping of axis line lengths at “lower”, “upper”, or “both” in addition to the default “none”. In short, capping at the upper/lower extremes halts drawing beyond the respective tick mark and the “both” setting would restrict the length of the axis line to the upper and lower limits (i.e., the desired behavior for single-case design figures).
An example of this expanded functionality is provided below.
ggplot(data, aes(x = Session,
y = CTB,
shape = Condition,
fill = Condition)) +
geom_line() +
geom_point(size = 5) +
scale_shape_manual(values = c("Toy Play" = 16,
"Attention" = 22,
"Demand" = 24,
"Tangible" = 8)) +
scale_fill_manual(values = c("Toy Play" = "black",
"Attention" = "white",
"Demand" = "white",
"Tangible" = "black")) +
scale_x_continuous(breaks = 1:15,
limits = c(1, 15),
# Note: see override of default behavior
guide = guide_axis(cap = "both")) +
scale_y_continuous(breaks = c(0, 0.5, 1, 1.5),
limits = c(0, 1.5),
# Note: see override of default behavior
guide = guide_axis(cap = "both")) +
theme_classic() +
theme(
text = element_text(family = "Times New Roman", size = 14),
legend.position = "inside",
legend.position.inside = c(1, 1),
legend.justification.inside = c(1, 1)
)
Although not new to position scales, the spacing applied the min/max ticks and to plotting panel bounds can similarly be extended as well. This may be especially desirable to override when many sessions or data points crowd the extremes of a panel. The is made possible by overriding the expand field in the position scale. By default, ggplot will assume that a 5% of plot width/height will serve as an appropriate degree of padding. The expand field lets you choose this amount by either an additive (add) or multiplicative (mult) value.
For our purposes, the desired result can be done with either, but for convenience, a more trim spacing is prepared and documented below. Although the default version is probably sufficient (perhaps even preferred), the case illustrated below serves to highlight the flexibility provided within ggplot to accomplish the desired visual products.
ggplot(data, aes(x = Session,
y = CTB,
shape = Condition,
fill = Condition)) +
geom_line() +
geom_point(size = 5) +
scale_shape_manual(values = c("Toy Play" = 16,
"Attention" = 22,
"Demand" = 24,
"Tangible" = 8)) +
scale_fill_manual(values = c("Toy Play" = "black",
"Attention" = "white",
"Demand" = "white",
"Tangible" = "black")) +
scale_x_continuous(breaks = 1:15,
limits = c(1, 15),
guide = guide_axis(cap = "both"),
# Note: Overriding the default % allotted to lower/upper extremes
expand = expansion(mult = c(0.03, 0.03))) +
scale_y_continuous(breaks = c(0, 0.5, 1, 1.5),
limits = c(0, 1.5),
guide = guide_axis(cap = "both"),
# Note: Overriding the default % allotted to lower/upper extremes
expand = expansion(mult = c(0.03, 0.03))) +
theme_classic() +
theme(
text = element_text(family = "Times New Roman", size = 14),
legend.position = "inside",
legend.position.inside = c(1, 1),
legend.justification.inside = c(1, 1)
)
Summary and Takeaways
The ggplot package is continuously improving, and for single-plot visuals, is entirely suited to producing publication-quality figures that can meet the standards of publishers of single-case design research. Although increasingly robust, the ggplot framework does not yet include annotations that extend across plots in a figure, and that remains one of the very few remaining barriers to being a complete solution to producing single-case design figures purely from an R file.