【问题标题】:How to change the order of facets when using gather? [duplicate]使用聚集时如何更改构面的顺序? [复制]
【发布时间】:2021-02-18 15:43:00
【问题描述】:

如何在使用功能聚集时手动更改构面的顺序?网格中的数字不是按照我在 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)

提前致谢!

【问题讨论】:

标签: r ggplot2 tidyr facet-wrap gather


【解决方案1】:

要在管道中对Fixing the order of facets in 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-09-29
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
相关资源
最近更新 更多