ggplot facet_wrap不按字母R顺序排列构面 [英] ggplot facet_wrap not ordering facets alphabetically R

查看:90
本文介绍了ggplot facet_wrap不按字母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.

我正在根据昨晚以人为中心的睡眠满意度来预测主观幸福感(即正面影响,负面影响和生活满意度).我想出了三个模型,现在我想将它们彼此并排绘制.问题在于facet_wrap将模型按字母顺序放置,而不是我想要的方式(正向影响,负向影响和生活满意度).

I'm predicting subjective well-being (i.e. 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).

您可以在此处查看我当前的图形

这是我使图形运行的代码:

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

在这里,我将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)

这是我的图形代码:

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

当我给tmp $ DV的因数一个不同的名字,即tmp $ facet并将其添加到我的代码中时,我确实得到了正确的顺序,但是刻度在y轴上不再自由了.请在这里看看.

When I give the factor of tmp$DV a different name, i.e. 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")

当我在第一行中将pivot_longer更改为facet时,得到的图形与之前的图形相同.

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.

祝一切顺利,阿妮塔

推荐答案

只要有人想知道,我的同事亨里克·辛格曼(Henrik Singmann)都能得到答案.

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

因此,在将因子移交给ggplot之前,需要在d3中对其进行定义.

So the factor needs to be defined in d3 before being handed over to ggplot.

这篇关于ggplot facet_wrap不按字母R顺序排列构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆