【问题标题】:Force facet_grid to plot facets in the same order as they appear in the data set强制 facet_grid 以与它们在数据集中出现的顺序相同的顺序绘制构面
【发布时间】:2019-09-30 20:18:19
【问题描述】:

我正在研究单个案例研究的视觉表示。我需要对 ggplot2 中的图表进行一些更改,但我发现这有点挑战性。以下是我用来制作可重现示例的玩具数据集中包含的变量的简要说明:

  • 场合:评估行为的会话评估者的数量(从 1 到 n);
  • 时间:每个条件的数量(基线从 1 到 n,干预从 1 到 n);
  • 阶段:条件(A = 基线或 B = 干预);
  • ID:学习中的学生代码
  • 结果:行为检查表的总分。

根据数据集中的标准(即第一次干预会话)对案例进行排序。不幸的是,当我使用ggplot2::facet_grid 创建不同的方面时,这些案例按编号排序,我得到了您在下图中看到的内容。我试图改变变量类型(从整数到因子,从因子到字符等),但似乎没有任何改变。最后,我无法手动对构面进行排序,因为真实数据集包含更多案例。

outcome <- c(4, 8, 10, NA, 15, 7, 7, 9, 14, NA, 16, 4, 3, 2, 2, 7, 7, 9, 14, NA, 3, 6, 6, NA, 5, 9, 11, NA, 6, 3, 4, 8, 7, NA, NA, 3)
Phase <- c("A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A", "A", "B", "B", "B", "B", "B", "A", "A", "A", "A", "B", "B", "B", "B", "A", "A", "A", "A", "B", "B", "B", "B", "B")
Time <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5)
Occasion <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9)
ID <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2)


db <- data.frame(ID, Occasion, Time, Phase, outcome)

intervention_lines <- db %>% 
  filter(Phase == "A") %>% 
  group_by(ID, Phase) %>%
  summarise(y = max(Occasion)) 

db %>% na.omit(outcome) %>% 
  ggplot(aes(x = Occasion, y = outcome, group = Phase)) + 
  geom_point(size = 1.8) + 
  geom_line(size = 0.65) +
  facet_grid(ID ~ .) +
  scale_x_continuous(name = "y", breaks = seq(0, 11, 1)) +
  scale_y_continuous(name = "x", limits = c(0, 30)) +
  theme_classic() +
  theme(strip.background = element_blank()) +
  annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf) +
  geom_vline(data = intervention_lines, aes(xintercept = y + 0.5), colour = "black", linetype = "dashed")

【问题讨论】:

  • 每当我在使用ggplot2时听到有人问“如何订购...”,答案总是回到factor(..., levels=c(...))。关键是factor(x) 不保留数据中的顺序(请参阅levels(factor(c('a','c','b')))),因此您必须显式提供factor(.., levels=...)levels(fctr) &lt;- c(...) 的顺序。
  • 能否请您说明您想要什么顺序以及如何仅从数据中获得该顺序?
  • @JonSpring 可以从对象intervention_lines 中获取信息,并从最小值到最大值重新排列;但是,为了使我的示例简单,我从一开始就以这种方式订购了这些箱子。所以在这种情况下,顺序是 1、7、2 和 3
  • @r2evans 感谢您的回答!您是否碰巧知道使用您建议的方法而无需指定名称的方法?也许创建一个具有案例唯一值的对象?我在绘制图表之前尝试使用这个mutate_at(vars(ID), funs(factor(., levels = unique(.)))) %&gt;% ,但它并没有改变任何东西。也许我在那行代码中遗漏了一些东西?

标签: r ggplot2 dplyr


【解决方案1】:

我遇到了一些麻烦,因为您的绘图为不同的层使用了两个数据框,并且它们需要匹配因子才能使构面排序起作用。

我通过将 ID 转换为一个因子,然后在两个地方按 intervention_lines$y 对其进行排序来做到这一点。

library(forcats)
intervention_lines <- db %>% 
  filter(Phase == "A") %>% 
  group_by(ID, Phase) %>%
  summarise(y = max(Occasion)) %>%
  ungroup() %>%
  mutate(ID = ID %>% as_factor() %>% fct_reorder(y))

db %>% na.omit(outcome) %>% 
  mutate(ID = as_factor(ID)) %>%
  left_join(intervention_lines %>% select(ID, y)) %>%
  mutate(ID = ID %>% fct_reorder(y)) %>% 
  ggplot(aes(x = Occasion, y = outcome, group = Phase)) + 
  geom_point(size = 1.8) + 
  geom_line(size = 0.65) +
  scale_x_continuous(name = "y", breaks = seq(0, 11, 1)) +
  scale_y_continuous(name = "x", limits = c(0, 30)) +
  theme_classic() +
  theme(strip.background = element_blank()) +
  annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf) +
  geom_vline(data = intervention_lines, aes(xintercept = y + 0.5), colour = "black", linetype = "dashed") +
  facet_grid(ID~.)

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    相关资源
    最近更新 更多