I am new to R. I want to plot 4 box plots for 4 continuous variables and present them in the same plot. I am trying to present the boxplot for each variable in 2 study groups while using facet_wrap in ggplot.
dividing variable is: cognitive_groups (has two values 0, 1) the 4 variables are: memory (presented here), attention, exeuctive and language domains. here is the code,
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")
Here is the output, How do I present the other variables alongside the memory? THANKS!
CodePudding user response:
Do you mean like this? A tribble
by the way is a nice way to create a minimal sample of data.
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"
)
Created on 2022-04-20 by the reprex package (v2.0.1)