Unwanted layout order numbers appearing in facet_wrap when specifying layout

28 Views Asked by At

I wanted to specify the layout of the facets when making a plot with face_wrap in ggplot, so I used the following code:

df$layout_order <- factor(rep(1:ceiling(nrow(df)/4), each = 4), levels = unique(rep(1:ceiling(nrow(df)/4), each = 4))[1:nrow(df)])

ggplot(df, aes(x = Time, y = nPVI, group = Time, fill = Group)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(title = "nPVI scores by Participant, Time and Group",
       x = "Time",
       y = "nPVI",
       group = "Time") +
  facet_wrap(~ Participant + layout_order, scales = "free", ncol = 4)+
  coord_cartesian(ylim = c(20, 60)) +
  scale_fill_manual(values = c("Oral" = "#86CB92", "Prosody" = "#0E7C7B")) +
  theme_minimal()

It works, but it also adds the layout order numbers at the top of each facet, under the Participant names. How can I remove them?

1

There are 1 best solutions below

1
Carl On BEST ANSWER

You could use labeller per below to suppress the layout_order.

(It is better if you can supply a small amount of sample data with the question.)

library(tidyverse)

df <- tibble(
  Time = 1:8,
  nPVI = 21:28,
  Group = rep(letters[1:4], 2),
  Participant = c("Bob", "Jill", "Sam", "Rose", "John", "Jane", "Tom", "Gail"),
  layout_order = 1:8
)


df$layout_order <- factor(rep(1:ceiling(nrow(df) / 4), each = 4), levels = unique(rep(1:ceiling(nrow(df) / 4), each = 4))[1:nrow(df)])

ggplot(df, aes(x = Time, y = nPVI, group = Time, fill = Group)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(
    title = "nPVI scores by Participant, Time and Group",
    x = "Time",
    y = "nPVI",
    group = "Time"
  ) +
  facet_wrap(~ Participant + layout_order,
             scales = "free", ncol = 4,
             labeller = labeller(layout_order = \(x) rep("", length(x)))
  ) +
  coord_cartesian(ylim = c(20, 60)) +
  scale_fill_manual(values = c("Oral" = "#86CB92", "Prosody" = "#0E7C7B")) +
  theme_minimal()

Created on 2024-03-27 with reprex v2.1.0