0

如何在使用功能收集时手动更改构面的顺序?网格中的数字不是按照我在 myvars 中定义的顺序显示的。

这是代码:

myvars <- c("1","2","3","4","5"
            ,"6","7","8","9","10",
            "11","12","13","14")
DataVars<- Data[myvars]

DataVars%>%
  gather(-1, key = "var", value = "value") %>% 
  ggplot(aes(x = value, y = 1)) +
  facet_wrap(~ var, scales = "free") +
  geom_jitter(size = 0.5, shape = 16, aes(colour = var),width = 0.5, height = 0.5) +
  geom_smooth(method="lm", size = 1, color = "black", level=0.95)

提前致谢!

4

1 回答 1

0

要使用公认的分析器在管道中修复 ggplot中的构面顺序,您需要mutate动词:

DataVars%>%
  gather(-1, key = "var", value = "value") %>% 
  mutate(var = factor(var, levels = ...) %>% 
  ggplot(aes(x = value, y = 1)) +
  facet_wrap(~ var, scales = "free") +
  geom_jitter(size = 0.5, shape = 16, aes(colour = var),width = 0.5, height = 0.5) +
  geom_smooth(method="lm", size = 1, color = "black", level=0.95)

在上面的示例中,将 替换为...包含所需刻面标签顺序的向量。

于 2021-02-18T16:54:02.810 回答