如何使用 ggpubr 从数据框中同时按行和列对图进行分面?

How to facet a plot by row and columns simultaneously from a data frame using ggpubr?

我不是 R 专家,我在绘制数据时遇到了一些麻烦

一些背景: 进行了一项使用 2 种混凝剂、3 种不同速度和 3 种不同固体浓度的实验,以观察 3 个参数的行为。实验结果记录在以下数据框中:

Coagulant <- c(rep("Chem_1", times = 9),
               rep("Chem_2", times = 9))

Velocity <- rep(c(rep("low", times = 3),
              rep("mid", times = 3),
              rep("high", times = 3)),
              times = 2)

Solids <- rep(c(0,50,100), times = 6)

Parameter_1 <- runif(n = 18,
                     min = 70,
                     max = 100)

Parameter_2 <- runif(n = 18,
                     min = 90,
                     max = 100)

Parameter_3 <- runif(n = 18,
                     min = 0,
                     max = 100)

Ex.data.frame <- data.frame(Coagulant,
                            Velocity,
                            Solids, 
                            Parameter_1,
                            Parameter_2,
                            Parameter_3)

Ex.data.frame$Coagulant <- factor(Ex.data.frame$Coagulant,
                                    levels = c("Chem_1", "Chem_2"),
                                    labels = c("Chem_1", "Chem_2"))

Ex.data.frame$Velocity <- factor(Ex.data.frame$Velocity,
                             levels = c("low", "mid", "high"),
                             labels = c("low", "mid", "high"))

Ex.data.frame$Solids <- factor(Ex.data.frame$Solids,
                                  levels = c(0, 50, 100),
                                  labels = c(0, 50, 100))

我想绘制此面板分离后的数据:

试了很多,只能提供这段代码来制作剧情:

require(ggpubr)

    ggbarplot(data = Ex.data.frame,
              y = "Parameter_1", #I'd like to use for all parameters (same units)
              fill = "Solids",
              position = position_dodge2(),
              facet.by = c("Coagulant", "??")) #Facet by the parameters columns from the data frame

这是实现您的目标的潜在方法(您只需切换构面以具有与图表中相同的列和线):

library(dplyr)
library(magrittr)
d <-rbind(Ex.data.frame[1:4] %>% rename(Parameter = Parameter_1) %>% mutate(param = "Parameter_1"), 
      Ex.data.frame[c(1:3,5)] %>% rename(Parameter = Parameter_2) %>% mutate(param = "Parameter_2"), 
      Ex.data.frame[c(1:3,6)]  %>% rename(Parameter = Parameter_3) %>% mutate(param = "Parameter_3")
      )

ggbarplot(data = d,
          y = c("Parameter" ), 
          fill = "Solids",
          position = position_dodge2(),
          facet.by = c( "param", "Coagulant" ))