如何在R 2020的一张图中绘制多个箱线图

时间:2020-09-18 03:10:00

标签: r

我有一些数据,需要在一张图中绘制多个箱形图。对于Rstudio 3.6.1,没有ggplot。请使用可用的软件包。数据文件为csv,具有740行。在这里,我削减了前20行。

   Absenteeism.time.in.hours Social.smoker Social.drinker
1                          4             0              1
2                          0             0              1
3                          2             0              1
4                          4             1              1
5                          2             0              1
6                          2             0              1
7                          8             0              1
8                          4             0              1
9                         40             0              1
10                         8             0              0
11                         8             0              1
12                         8             0              1
13                         8             0              1
14                         1             0              1
15                         4             0              1
16                         8             0              1
17                         2             0              1
18                         8             1              1
19                         8             0              0
20                         2             1              0

2 个答案:

答案 0 :(得分:2)

仅使用boxplot有什么问题?否ggplot2,并且在您的版本中也应如此。

但是,“一张图中的多个箱形图”的含义并不明确。这里是三个版本:

## by social group
op <- par(mfrow=c(1, 2))  ## set par
boxplot(Absenteeism.time.in.hours ~ Social.smoker, dat)
boxplot(Absenteeism.time.in.hours ~ Social.drinker, dat)
par(op)  ## reset par

enter image description here

## by social group in one panel 
datl <- reshape(dat, varying=2:3, direction="long")
boxplot(Absenteeism.time.in.hours ~ time + Social, datl)

enter image description here

## social group interaction
boxplot(Absenteeism.time.in.hours ~ ., dat)

enter image description here


数据:

dat <- read.table(header=T, text="   Absenteeism.time.in.hours Social.smoker Social.drinker
1                          4             0              1
2                          0             0              1
3                          2             0              1
4                          4             1              1
5                          2             0              1
6                          2             0              1
7                          8             0              1
8                          4             0              1
9                         40             0              1
10                         8             0              0
11                         8             0              1
12                         8             0              1
13                         8             0              1
14                         1             0              1
15                         4             0              1
16                         8             0              1
17                         2             0              1
18                         8             1              1
19                         8             0              0
20                         2             1              0")

答案 1 :(得分:1)

@ jay.sf的解决方案非常棒(我相信他将是在没有settings.json的世界中唯一能够通过惊人的可视化生存的人)。正如伟大的@ r2evans在评论中所提到的,使ggplot2工作的关键是重塑数据。一种方法是像您一样使用ggplot2函数。但是使用实际的reshape2函数更容易将数据分析管道直接连接到可视输出。在这里,我将介绍一种使用tidyverse来重整数据的方法,并使用tidyverse将数据整形为pivot_wider()。如果需要多个绘图,可以使用ggplot2来为绘图提供矩阵样式。这里的代码将变量facet_wrap()用作键,以重塑其他二进制变量:

Absenteeism.time.in.hours

输出:

enter image description here

由于@ jay.sf处理了数据library(tidyverse) #Code dat %>% pivot_longer(cols = -Absenteeism.time.in.hours) %>% ggplot(aes(x=factor(value),y=Absenteeism.time.in.hours))+ geom_boxplot()+ facet_wrap(.~name,scales = 'free')+ theme_bw()+ xlab('Var') (所有数据管理工作都归功于他),因此产生了此代码。