使用 dplyr 将数据转换为长格式时修复 ggplot 中的方面顺序

Fixing the order of facets in ggplot when using dplyr to transform data to long form

昨天我在这里得到了帮助,创建了一个多列的分面网格。这产生了一个包含 8*5 图的大网格。该代码为各种 Outcomes * Responses 创建了 plot 的组合。例如(结果 1 * Response1、结果 1 * Response2、结果 3 * Response1、结果 2 * Response1 等等)。

我已经粘贴了下面的代码。

plot1 <- ancestralmeansindex %>%
gather(var1, value1, bicepind:wcind) %>%
gather(var2, value2, mmois:mpfat) %>%
ggplot(aes(x = value1, y = value2)) + 
geom_point(color='blue') +
geom_smooth(method = "lm", se = FALSE) +
facet_grid(var2 ~ var1, scales = "free", switch = "both",
         labeller = as_labeller(c(mmois = "Water (gms)",
                                  mkcal = "Caloric Intake",
                                  mprot = "Protein (gms)",
                                  mcarb = "Carb (gms)",
                                  mtfat = "Total Fat (gms)",
                                  msfat = "Saturated Fat (gms)",
                                  mmfat = "Mono S.Fat (gms)",
                                  mpfat = "Poly US.Fat (gms)",
                                  bicepind = "Bicep",
                                  tricepind = "Tricep",
                                  subind = "Subscapular",
                                  supind = "Suprailiac",
                                  weightind = "Weight",
                                  wcind = "Waist Circum"))) +
  labs(title = "Regression Plot Matrix of Mean Dietary Values with    Index Change 1", x = NULL, y = NULL) +
theme_bw() +
theme(strip.placement = "outside",
    strip.background = element_blank())
ggsave("Regression Plot 1.pdf", width = 210, height = 297, units = "mm", plot1) 

这给出了代码中提到的所有可能组合的非常整洁的网格。但是,该图按字母顺序打印图(正如 labeller/data 中所反映的那样)。我想更改 var2 和 var1 的顺序。

我在帮助中看到这个问题可以通过分配因子水平和选择给定顺序来解决。比如这个解Fixing the order of facets in ggplot

如何将因子水平分配给已被 dplyr 转换为长格式的变量?这可以做到吗?还有其他解决方案吗?

Edit1 我尝试了下面的解决方案,但我 运行 遇到了错误。下面的可重现示例。

set.seed(1)
dat <- data.frame(
  Outcome1 = sample(1:10),
  Outcome2 = sample(11:20),
  Outcome3 = sample(21:30),
  Response1 = sample(31:40),
  Response2 = sample(41:50),
  Response3 = sample(51:60)
)

dat %>%
  gather(var1, value1, Outcome1:Outcome3) %>%
  mutate(var1, recode("Outcome1" = "Bicep",
                      "Outcome2" = "Tricep",
                      "Outcome3" = "Subscapular")) %>%
  factor(var1, levels = c("Bicep",
                          "Tricep",
                          "Subscapular")) 
gather(var2, value2, Response1:Response3) %>% 
  mutate(var2, recode("Response1" = "Water (gms)", 
                      "Response2" = "Caloric Intake",
                      "Response3" = "Protein (gms)")) %>%
  factor(var2, levels = c("Water (cms)", 
                          "Caloric Intake",
                          "Protein (gms)")) %>%
  ggplot(aes(x = value1, y = value2)) + 
  geom_point(color='blue') +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(var2 ~ var1, scales = "free", switch = "both",
             labeller = as_labeller(c(mmois = "Water (gms)",
                                      mkcal = "Caloric Intake",
                                      mprot = "Protein (gms)",
                                      bicepind = "Bicep",
                                      tricepind = "Tricep",
                                      subind = "Subscapular"))) +
  labs(title = "Regression Plot", x = NULL, y = NULL) +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank())


Error in factor(., var1, levels = c("Bicep", "Tricep", "Subscapular",  : 
  object 'var1' not found

Error in gather(var2, value2, Response1:Response3) : 
  object 'var2' not found

是的!可以办到。使用 dplyr mutaterecode 您的 var1 和 var2 变量,然后使用 factor 确保级别顺序正确。那么您就不需要使用贴标机了。感谢您的可重现示例,我能够测试我的解决方案并修复代码!

 dat %>%
  gather(var1, value1, Outcome1:Outcome3) %>%
  mutate(var1 = recode(var1, "Outcome1" = "Bicep",
                      "Outcome2" = "Tricep",
                      "Outcome3" = "Subscapular")) %>%
  mutate(var1 = factor(var1, levels = c("Bicep",
                          "Tricep",
                          "Subscapular"))) %>% 
gather(var2, value2, Response1:Response3) %>% 
  mutate(var2 = recode(var2, "Response1" = "Water (gms)", 
                      "Response2" = "Caloric Intake",
                      "Response3" = "Protein (gms)"),
          var2 = factor(var2, levels = c("Water (gms)", 
                          "Caloric Intake",
                          "Protein (gms)"))) %>%
  ggplot(aes(x = value1, y = value2)) + 
  geom_point(color='blue') +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(var2 ~ var1, scales = "free", switch = "both") +
  labs(title = "Regression Plot", x = NULL, y = NULL) +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank())