使用相同 facet_wrap 的不同变量的多个箱线图

Multiple boxplot for different variable using same facet_wrap

我是 R 的新手。 我想为 4 个连续变量绘制 4 个箱线图,并将它们呈现在同一个图中。我正在尝试在 ggplot.

中使用 facet_wrap 时在 2 个研究组中呈现每个变量的箱线图

除法变量是:cognitive_groups(有两个值 0、1) 这 4 个变量是:记忆(此处显示)、注意力、执行力和语言领域。 这是代码,

cogdb_bl%>%
filter(!is.na(cognitive_groups))%>%
ggplot(aes(x=memory))+
geom_boxplot(aes(y=""))+
facet_wrap(~cognitive_groups)+
theme_bw()+
coord_flip()+
labs(title="Cognitive domains in baseline groups",
   x="Z score")    

这是输出, 我如何在内存旁边显示其他变量? 谢谢!

你是这个意思吗?顺便说一下,tribble 是创建最小数据样本的好方法。

library(tidyverse)

tribble(
  ~participant, ~memory, ~attention, ~language, ~executive, ~cognitive,
  "A", 2, 5, 2, 2, 0,
  "B", 2, 2, 5, 2, 1,
  "C", 2, 2, 2, 2, 0,
  "D", 2, 3, 2, 6, 1,
  "E", 2, 2, 2, 2, 0,
  "F", 2, 2, 8, 2, 0,
  "G", 2, 4, 2, 2, 1,
  "H", 2, 2, 7, 2, 1
) |>
  pivot_longer(c(memory, attention, language, executive), 
               names_to = "domain", values_to = "score") |>
  ggplot(aes(domain, score)) +
  geom_boxplot() +
  facet_wrap(~cognitive) +
  theme_bw() +
  coord_flip() +
  labs(
    title = "Cognitive domains in baseline groups",
    y = "Z score"
  )

reprex package (v2.0.1)

于 2022-04-20 创建