I trying to use the trick of using two calls to annotate() to get 2-level nested tick labels (here) BUT I want to do this in conjunction with facet_wrap()
df <- tribble(
~action, ~gend, ~status, ~cellMean,
"P", "M", "A", 1,
"P", "M", "B", 2,
"P", "F", "A", 1.4,
"P", "F", "B", 2.6,
"V", "M", "A", 2,
"V", "M", "B", 3,
"V", "F", "A", 2.2,
"V", "F", "B", 3.8
) %>%
mutate(action=factor(action,levels=c("P","V")),
gend =factor(gend, levels=c("F","M")),
status=factor(status,levels=c("A","B")))
df
# action obscured/overlaid
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
theme_light()
# action used as facet
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
theme_light() +
facet_wrap(~action)
# annotate hack on unfaceted
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) ) +
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
theme_light() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() )
# annotate hack on FACETED fails saying it wants 8 labels
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) ) +
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
theme_light() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() ) +
facet_wrap(~action)
Produces: "Error: Aesthetics must be either length 1 or the same as the data (8): label", which seems to want 8 labels across two facets each with 4 labels.
But, attempts to supply 8 only then demand 16.
# annotate hack on FACETED with length 8 vectors fails saying 16
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
annotate(geom = "text", x = rep(1:4, times=2), y = .7, label = rep(c("A","B"), times=4) ) +
annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
theme_light() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank() ) +
facet_wrap(~action)
Produces: "Error: Aesthetics must be either length 1 or the same as the data (16): label"
Is there a way to use the annotate() trick with facet_wrap()?
I wonder if I need to make 2 plots and put them side by side to simulate facets.