Yamlet Plot Aesthetics"

Behavior

Plotting a decorated data.frame in ggplot() has special behavior for aesthetics defined using yamlet decorations. If there is only one dataset (typical), the aesthetics are coordinated with the (levels of the) corresponding variable.

If one or more additional layers provide non-default data, a single set of consensus decorations is accumulated, with each successive dataset overriding all previous decorations where conflicts exist; then, consensus decorations are enforced on all datasets.

Example

In the example below, points are plotted using the observed data, then summary data is plotted as a separate layer, supplying its own data. the plot is rendered without decorations (left) and with decorations (right).

#| label: Layered Aesthetics
#| fig.width: 5.02
#| fig.height: 3.28

library(magrittr)
library(ggplot2)
library(yamlet)
library(dplyr)
library(metaplot)

set.seed(10)
obs <- data.frame(
  time = 1:100,
  group = c('a','b'),
  concentration = rnorm(100)
)

# observed data
obs %<>% decorate('
  time: [ Time, h ]
  concentration: [ Concentration, ng/mL ]
  group: [ 
    Group, 
    [ a, b ], 
    color: [ green, yellow ] , 
    linetype: [ dashed, dotdash ]
  ]
')

# summary data
sum <- obs %>% group_by(group) %>% summarize(mean = mean(concentration))
sum %<>% undecorate %>% decorate('
  group: [ 
    Subset, 
    [ a, b ], 
    color: [ blue, magenta ], 
    shape: [ 15, 19 ]
  ]
')


q <- obs %>%
  undecorate %>%
  ggplot(aes(time, concentration)) +
  geom_point(aes(color = group, shape = group)) +
  geom_hline(
    data = undecorate(sum), 
    aes(
      yintercept = mean, 
      color = group, 
      linetype = group
    )
  ) + 
  theme(legend.position = "top") +
  labs(subtitle = 'without decorations') +
  symmetric()

p <- obs %>%
  resolve %>%
  ggplot(aes(time, concentration)) +
  geom_point(aes(color = group, shape = group)) +
  geom_hline(
    data = resolve(sum), 
    aes(
      yintercept = mean, 
      color = group, 
      linetype = group
    )
  ) + 
  theme(legend.position = "top") +
  labs(subtitle = 'with decorations') + 
  symmetric()


multiplot(q, p) # %>% devsize(2,2)

This example is a bit contrived for heuristic value. As a best practice, consider specifying all aesthetics in the default data only.

sessionInfo()


Try the yamlet package in your browser

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

yamlet documentation built on Jan. 10, 2026, 9:07 a.m.