我是 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")
這是輸出,如何在記憶體旁邊顯示其他變數?謝謝!
uj5u.com熱心網友回復:
你的意思是這樣嗎?順便說一句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 包于 2022-04-20 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462732.html
上一篇:從納稅年度中提取日歷年