简体   繁体   中英

How to change the number of plots in each row with facet_wrap in ggplot2?

I have a multiple barplot that drawing by ggplot2 in the form of 4x4, but I want to have 3 plots in each row (5x3 and 1 extra row). How can I reorder the plots? Thanks for any help.

Here is my script

nudge <- 0.1
df |> 
  mutate(
    sig_label = case_when(
      padj < 0.001 ~ "***",
      padj < 0.01 ~ "**",
      padj <= 0.05 ~ "*",
      padj > 0.05 ~ ""
    ),
    label_position = ifelse(log2FC > 0, log2FC + nudge, log2FC - (nudge+0.07))
  ) |> 
  ggplot(aes(x = CancerType, y = log2FC , fill = Genes)) +
  geom_bar(stat = "identity" , width = 0.7,show.legend = T)+ theme_bw() + facet_wrap(~ Genes)+
  theme(axis.text.x = element_text(size =9, angle = 45, hjust = 1 , colour = "black"),
        panel.border = element_rect(color="black", linewidth = 1.5),
        strip.background = element_rect(
          color="black", fill="lightgray", size=1.5, linetype="solid"),
        strip.text.x = element_text(size = 12, color = "black", face = "bold.italic"),
        axis.text.y = element_text(size = 15,  colour = 'black',face = 'bold')) +
  geom_text(aes(label = sig_label, y = label_position), size= 6)+
  geom_hline(yintercept=0.0,color='black', size=0.8)

在此处输入图像描述

You can use facet_manual from the ggh4x package

library(ggh4x)

mat <- matrix(c(1, 1, 2, 2, 3, 3, 
                4, 4, 5, 5, 6, 6, 
                7, 7, 8, 8, 9, 9,
                10, 10, 11, 11, 12, 12,
                13, 13, 14, 14, 15, 15,
                16, 16, NA, NA, NA, NA),
                6, 6, byrow = TRUE)

nudge <- 0.1
df |> 
  mutate(
    sig_label = case_when(
      padj < 0.001 ~ "***",
      padj < 0.01 ~ "**",
      padj <= 0.05 ~ "*",
      padj > 0.05 ~ ""
    ),
    label_position = ifelse(log2FC > 0, log2FC + nudge, log2FC - (nudge+0.07))
  ) |> 
  ggplot(aes(x = CancerType, y = log2FC , fill = Genes)) +
  geom_bar(stat = "identity" , width = 0.7,show.legend = T)+ 
  theme_bw() + 
  facet_manual(vars(Genes), design = mat) +
  theme(axis.text.x = element_text(size =9, angle = 45, hjust = 1 , colour = "black"),
        panel.border = element_rect(color="black", linewidth = 1.5),
        strip.background = element_rect(
          color="black", fill="lightgray", size=1.5, linetype="solid"),
        strip.text.x = element_text(size = 12, color = "black", face = "bold.italic"),
        axis.text.y = element_text(size = 15,  colour = 'black',face = 'bold')) +
  geom_text(aes(label = sig_label, y = label_position), size= 6)+
  geom_hline(yintercept=0.0,color='black', size=0.8)

在此处输入图像描述


Data used

df <- data.frame(
  CancerType = rep(replicate(13, paste(sample(LETTERS, 4), collapse = "")), 16),
  padj = 10^sample(-5:0, 16*13, TRUE),
  log2FC = runif(16*13, -1.5, 1.5),
  Genes = rep(paste0("Gene", 1:13), each = 13)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM