我想知道如何繪制我在資料中看到的內容。
就背景關系而言,我有每個學生在 9 個不同科目中獲得的數字表現分數(我的快照僅包括其中的 4 個)。我有大約 50 名學生的資料。我感興趣的一個事實是,學??生的表現是否存在學科差異。例如,我想知道學生在所有科目中的表現是否始終如一,或者他們的表現是否存在差異。
我怎么能策劃這個?
student_id Math_score English_score Social_studies_score Chemistry_score
42 20 15 16 12
41 23 23 14 18
40 6 12 20 23
32 18 9 23 22
34 15 21 23 21
45 10 8 22 18
39 20 12 18 6
10 23 19 17 14
謝謝!
uj5u.com熱心網友回復:
這是一種方法,只是繪制原始資料,但通過按平均分數對學生進行排序來澄清事情。
首先,我使用 longtidyr::pivot_longer
對資料進行整形,這會將資料轉換為更適合 ggplot2 的格式。然后我采取步驟將學生 id 更改為按所有科目平均分數排序的有序因子。
library(tidyverse)
df_long <- df %>%
pivot_longer(-student_id) %>%
mutate(student_id = student_id %>% as.factor %>%
fct_reorder(value, mean) %>% fct_rev)
ggplot(df_long, aes(student_id, value, color = name))
geom_point()
資料:
df <- data.frame(
student_id = c(42L, 41L, 40L, 32L, 34L, 45L, 39L, 10L),
Math_score = c(20L, 23L, 6L, 18L, 15L, 10L, 20L, 23L),
English_score = c(15L, 23L, 12L, 9L, 21L, 8L, 12L, 19L),
Social_studies_score = c(16L, 14L, 20L, 23L, 23L, 22L, 18L, 17L),
Chemistry_score = c(12L, 18L, 23L, 22L, 21L, 18L, 6L, 14L)
)
uj5u.com熱心網友回復:
一個可以很好地擴展到更多科目和學生的選項是華夫餅式情節:
library(tidyverse)
pivot_longer(df, -1) %>%
mutate(Student = factor(student_id),
Subject = sub(' score', '', gsub('_', ' ', name))) %>%
ggplot(aes(Student, Subject, fill = value))
geom_tile(size = 3, color = 'white')
coord_equal()
labs(fill = 'Score')
scale_fill_viridis_c()
theme_void(base_size = 12)
theme(axis.text = element_text(),
axis.title = element_text(),
axis.text.y = element_text(hjust = 1),
axis.title.y = element_text(angle = 90, size = 20),
axis.title.x = element_text(margin = margin(10, 10, 10, 10),
size = 20),
legend.title = element_text(size = 20),
legend.margin = margin(20, 20, 20, 20))
擴展到 50 個學生和 10 個科目可能看起來像這樣:
uj5u.com熱心網友回復:
可用于geom_boxplot
按學生顯示跨學科分數的中位數和范圍。
通過添加geom_point
圖層,您可以額外顯示主題分數。
為了將其擴展到 500 名學生,我使用cut_interval
. 您可以選擇任意數量的組。
library(tidyverse)
library(glue)
# Sample data
df <- tibble(
student_id = 1:500,
Math_score = rnorm(500, 23, 1.2),
English_score = rnorm(500, 24, 1.3),
Social_studies_score = rnorm(500, 22, 1.4),
Chemistry_score = rnorm(500, 20, 1.1)
)
# Summarise and plot
df |>
pivot_longer(-student_id, values_to = "score", names_to = "subject") |>
group_by(student_id) |>
mutate(mean_score = mean(score)) |>
ungroup() |>
mutate(quartile = cut_interval(mean_score, n = 4,
labels = c("Bottom Quartile", "Lower Quartile",
"Upper Quartile", "Top Quartile")),
quartile = fct_reorder(quartile, mean_score)) |>
ggplot(aes(quartile, score, group = quartile))
geom_boxplot()
geom_jitter(aes(colour = subject), width = 0.2)
coord_flip()
labs(x = NULL, y = "Score", colour = "Subject",
title = glue("Scores for {nrow(df)} Students"))
theme_bw()
由reprex 包創建于 2022-06-02 (v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485495.html