简体   繁体   中英

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.

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. 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).

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. 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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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