使用dplyr将数据转换为长格式时,固定ggplot中构面的顺序

时间:2018-09-27 15:52:58

标签: r ggplot2 dplyr facet-grid

昨天我在这里得到了帮助,以创建多列的构面网格。这产生了一个包含8 * 5地块的大网格。该代码创建了针对各种结果*响应的情节组合。例如(结果1 *响应1,结果1 *响应2,结果3 *响应1,结果2 *响应1,依此类推)。

我粘贴了以下代码。

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) 

这给出了代码中提到的所有可能组合的非常整齐的网格。但是,该图按字母顺序打印图(因为它反映在贴标签机/数据中)。我想为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

1 个答案:

答案 0 :(得分:1)

是的!可以办到。使用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())