ggplot 中的 facet 函数如何用于创建直方图以可视化数据集中所有变量的分布?

How can the facet function in ggplot be used to create a histogram in order to visualize distributions for all variables in a dataset?

这是我想要可视化的变量示例

id  post.test.score  pre.test.score  messages  forum.posts  av.assignment.score
1        0.37             0.48         68          7               0.19
2        0.52             0.37         83         22               0.28
3        0.42             0.37         81          7               0.25
4        0.56             0.34         94         14               0.27
5        0.25             0.39         42         11               0.07

我已经从你上面的post复制了数据,所以你可以跳过变量赋值

library("tidyverse")

df <- read.table(file = "clipboard", header = T) %>% 
  as_tibble()

在将其传递给 ggplot 之前,您需要稍微修改一下数据结构。使用 tidyr::gather 将每个测试名称放入一个变量中。然后管道到 ggplot:

df %>% 
  gather(test, value, -id) %>% 
  ggplot(aes(x = value)) +
  geom_histogram() +
  facet_grid(~test)