简体   繁体   English

ggplot facet_wrap 不按字母顺序排列构面 R

[英]ggplot facet_wrap not ordering facets alphabetically R

I've spent the past few days looking through so many forums and sites, so I hope you can help.这几天浏览了很多论坛和网站,希望能帮到你。

You can find the data I've been using here , as well as the three model predictions.你可以在这里找到我一直使用的数据,以及三个 model 预测。

I'm predicting subjective well-being (ie positive affect, negative affect, and life satisfaction) from last night's person-centered sleep satisfaction.我从昨晚以人为中心的睡眠满意度预测主观幸福感(即积极影响、消极影响和生活满意度)。 I came up with three models that I now want to plot next to each other.我想出了三个模型,我现在想把 plot 彼此相邻。 The problem is that facet_wrap puts the models next to each other alphabetically and not how I want them (positive affect, negative affect, and life satisfaction).问题是 facet_wrap 将模型按字母顺序排列,而不是我想要的(积极影响、消极影响和生活满意度)。

You can view my current graph here你可以在这里查看我当前的图表

This is my code to get the graph going:这是我让图表运行的代码:

library("afex") 
library("tidyverse") 
library("tidylog") 
theme_set(theme_bw(base_size = 15)) 
library("sjPlot")

d3 <- read.csv("d3.csv")

d3 <- d3 %>% 
  group_by(ID) %>% 
  mutate(SD_person_centred = sleepDur - mean(sleepDur, na.rm = TRUE)) %>% 
  mutate(sleep_satisfaction_person_centred = Sleep_quality_open - mean(Sleep_quality_open, na.rm = TRUE)) %>%
  mutate(MS_person_centred = mid_sleep_modified - mean(mid_sleep_modified, na.rm = TRUE)) %>%
  mutate(MS_person_freeday_centred = abs(mid_sleep_modified - 
                                           mean(mid_sleep_modified[Routine_work_day_open == "No"], na.rm = TRUE))) %>%
  mutate(MS_person_mctq_centred = abs(mid_sleep_modified - MCTQ_MSF_number)) %>%
  mutate(sleep_onset_person_centred = Sleep_Onset_open - mean(Sleep_Onset_open, na.rm = TRUE)) %>%
  mutate(sleep_efficiency_person_centred = SleepEfficiency_act - mean(SleepEfficiency_act, na.rm = TRUE)) %>%
  ungroup

m_p_sls_1 <- readRDS("m_p_sls_1.rds")
m_n_sls_1 <- readRDS("m_n_sls_1.rds")
m_s_sls_1 <- readRDS("m_s_sls_1.rds")

tmp <- get_model_data(m_p_sls_1$full_model, type = "pred", terms = "sleep_satisfaction_person_centred")
tmp$DV <- "positive_affect"

tmp2 <- get_model_data(m_n_sls_1$full_model, type = "pred", terms = "sleep_satisfaction_person_centred")
tmp2$DV <- "negative_affect"

tmp3 <- get_model_data(m_s_sls_1$full_model, type = "pred", terms = "sleep_satisfaction_person_centred")
tmp3$DV <- "life_satisfaction"

tmp <- bind_rows(tmp, tmp2, tmp3)
tmp
tmp$DV

Here I change tmp$DV into a factor as this was the solution I found online.在这里,我将 tmp$DV 更改为一个因素,因为这是我在网上找到的解决方案。 However, this did not change anything:但是,这并没有改变任何东西:

tmp$DV <- factor(tmp$DV, levels=c("positive_affect","negative_affect","life_satisfaction"))
levels(tmp$DV)

This is my code for the graph:这是我的图表代码:

variable_names <- list(
  "positive_affect" = "positive affect" ,
  "negative_affect" = "negative affect",
  "life_satisfaction" = "life satisfaction"
)


variable_labeller <- function(variable,value){
  return(variable_names[value])
}

d3 %>% 
  pivot_longer(cols="positive_affect":"life_satisfaction", names_to = "DV", values_to = "Score") %>% 
  ggplot(aes(x = sleep_satisfaction_person_centred, y = Score)) +
  geom_ribbon(data = tmp, aes(x = x, ymin = conf.low, ymax = conf.high, y = predicted), 
              fill = "lightgrey") +
  geom_line(data = tmp, aes(x = x, y = predicted, group = 1)) +
  geom_point(alpha = 0.2) +
  facet_wrap(~DV, scales = "free_y",labeller=variable_labeller) +
  labs(y = "Score", 
       x = "Sleep satisfaction person centered")

When I give the factor of tmp$DV a different name, ie tmp$facet and add this to my code, I do get the right order, but the scales are not free on the y-axis anymore.当我给 tmp$DV 的因子一个不同的名称,即 tmp$facet 并将其添加到我的代码中时,我确实得到了正确的顺序,但是 y 轴上的比例不再是自由的了。 Please have a look here.请看这里。

tmp$facet <- factor(tmp$DV, levels=c("positive_affect", "negative_affect", "life_satisfaction"))

d3 %>% 
  pivot_longer(cols="positive_affect":"life_satisfaction", names_to = "DV", values_to = "Score") %>% 
  ggplot(aes(x = sleep_satisfaction_person_centred, y = Score)) +
  geom_ribbon(data = tmp, aes(x = x, ymin = conf.low, ymax = conf.high, y = predicted), 
              fill = "lightgrey") +
  geom_line(data = tmp, aes(x = x, y = predicted, group = 1)) +
  geom_point(alpha = 0.2) +
  facet_wrap(~facet, scales = "free_y",labeller=variable_labeller) +
  labs(y = "Score", 
       x = "Sleep satisfaction person centered")

When I change pivot_longer to facet in the first row, I get the same graph as the one before.当我将第一行中的 pivot_longer 更改为 facet 时,我得到的图表与之前的图表相同。

Sorry for the long post, but I tried to be as clear as possible.很抱歉这篇文章很长,但我试图尽可能清楚。 Please let me know if I wasn't.如果我不是,请告诉我。

I'd appreciate any kind of hints.我会很感激任何提示。 Thanks a lot for your time.非常感谢您的时间。

All the best, Anita一切顺利,安妮塔

Just got the answer from my colleague Henrik Singmann, in case anybody was wondering:刚刚从我的同事 Henrik Singmann 那里得到了答案,以防有人想知道:

d3 %>% 
  pivot_longer(cols="positive_affect":"life_satisfaction", names_to = "DV", values_to = "Score") %>% 
  mutate(DV = factor(DV, levels=c("positive_affect","negative_affect","life_satisfaction"))) %>%
  ggplot(aes(x = sleep_satisfaction_person_centred, y = Score)) +
  geom_ribbon(data = tmp, aes(x = x, ymin = conf.low, ymax = conf.high, y = predicted), 
              fill = "lightgrey") +
  geom_line(data = tmp, aes(x = x, y = predicted, group = 1)) +
  geom_point(alpha = 0.2) +
  facet_wrap(~DV, scales = "free_y",labeller=variable_labeller) +
  labs(y = "Score", 
       x = "Sleep satisfaction person centered")

So the factor needs to be defined in d3 before being handed over to ggplot.所以在交给ggplot之前需要在d3中定义因子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM