强制facet_grid以与数据集中显示的顺序相同的顺序绘制构面 [英] Force facet_grid to plot facets in the same order as they appear in the data set
问题描述
我正在研究单个案例研究的视觉表示。我需要对ggplot2中的图形进行一些更改,但是我发现这有些挑战。这是我用来制作可重现示例的玩具数据集中包含的变量的简要说明:
I am working on visual representations of single case studies. I need to make some changes to my graph in ggplot2, but I found this to be a bit challenging. Here is a brief description of the variables contained in the toy data set that I used to make a reproducible example:
- 场合:会话评估程序评估了行为(从1到n);
- 时间:每种情况的数量(基线从1到n,干预从1到n);
- 阶段:条件(A =基线或B =干预);
- ID:研究中的学生代码
- 结果:行为清单上的总分。
- Occasion: Number of the session rater evaluated the behavior (from 1 to n);
- Time: Number of each condition (baseline from 1 to n and intervention from 1 to n);
- Phase: Condition (A = baseline or B = intervention);
- ID: student code in the study
- outcome: total score on a behavioral checklist.
根据数据集中的标准(即第一次干预会话)对病例进行排序。不幸的是,当我用 ggplot2 :: facet_grid
创建了不同的构面时,案例按其编号排序,我在下图中看到了。我试图更改变量类型(从整数到因数,从因数到字符,等等),但是似乎没有什么改变。最后,由于实际数据集还包含其他几种情况,因此我无法手动订购这些方面。
The cases are ordered based on a criterion (i.e., the first intervention session) in the data set. Unfortunately, when I created different facets with ggplot2::facet_grid
, the cases are sorted by their number and I got what you can see in the image below. I tried to change the variable type (from integer to factor, from factor to character, etc.), but nothing seemed to change. Finally, I can't order the facets manually because the real data set consists of several more cases.
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")
推荐答案
我遇到了一些麻烦,因为您的绘图在不同图层上使用了两个数据框,因此它们需要
I ran into some trouble since your plot is using two data frames for different layers, and they need matching factors for the facet ordering to work.
我通过将ID转换为一个因子,然后通过 intervention_lines $ y $
I did that by converting ID to a factor and then ordering it by intervention_lines$y
in both places.
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~.)
< a href = https://i.stack.imgur.com/vmGLE.png rel = nofollow noreferrer>
这篇关于强制facet_grid以与数据集中显示的顺序相同的顺序绘制构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!