简体   繁体   English

如何为 R 中我的数据框的太多列写入 plot 直方图和箱线图的循环?

[英]How to write a loop to plot histograms and box-plots for too many colums of my Data frame in R?

I am new at using R.我是使用 R 的新手。

I have a Data frame of 80 colums(variables), each one with 100 observations (rows);我有一个包含 80 个列(变量)的数据框,每个列有 100 个观察值(行); all the observations are numeric.所有的观察都是数字的。 I need to plot histograms and box plots for each column (variable).我需要每列(变量)的 plot 直方图和箱线图。 I think the easier way is creating a loop, but I don't know how to do it.我认为更简单的方法是创建一个循环,但我不知道该怎么做。

Here is one approach to plotting histograms/boxplots with a similar 'sample' dataset:这是使用类似“样本”数据集绘制直方图/箱线图的一种方法:

library(ggplot2)
library(tidyr)

set.seed(1)

# create sample data
df <- data.frame(matrix(sample(1:1000, size = 8000, replace = TRUE), 
                        ncol = 80, 
                        nrow = 100, 
                        dimnames = list(c(), paste0("Variable_", 1:80))))
# check format of first 5 columns and 5 rows
head(df[,1:5], n = 5)
#>   Variable_1 Variable_2 Variable_3 Variable_4 Variable_5
#> 1        836        620        218        441        464
#> 2        679        304        610        294        674
#> 3        129        545        194         62        733
#> 4        930        557         19        390        493
#> 5        509        661        273        644        675

# reformat the data to 'long' format (https://tidyr.tidyverse.org/reference/pivot_longer.html)
df_long <- pivot_longer(df, everything(), names_to = "variable", values_to = "observation")

# specify that 'Variables' are ordered (i.e. Variable_2 is after Variable_1)
df_long$variable <- factor(df_long$variable, levels = unique(df_long$variable), ordered = TRUE)

# plot histograms with ggplot
ggplot(df_long, aes(x = observation)) +
  geom_histogram(bins = 15) +
  facet_wrap(~variable, ncol = 6, strip.position = "right")

# plot boxplots with ggplot
ggplot(df_long, aes(x = variable, y = observation)) +
  geom_boxplot(outlier.shape = NA) +
  theme(axis.text.x = element_text(angle = 90))

Created on 2022-01-28 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 1 月 28 日创建

These figures are 'squashed', but you can resize them to see the axes/text properly when you export or save.这些数字被“压扁”,但您可以在导出或保存时调整它们的大小以正确查看轴/文本。 Does this solve your problem?这能解决你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM