当因子有3个级别时,为什么我在R上的箱线图出现1个盒子?

  • 本文关键字:盒子 1个 3个 r
  • 更新时间 :
  • 英文 :


我正试图对一些数据进行嵌套方差分析测试,并一直遵循R教程。为了使数据可视化,我创建了一个箱线图,但是只有一个框出现在x轴上表示"位置"。当数据中有3个位置时

所有数据已使用"as.factor"转换为因子

> str(ANOVADATArobin)
tibble [105 × 4] (S3: tbl_df/tbl/data.frame)
$ location  : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
$ site      : Factor w/ 12 levels "1","2","3","4",..: 1 1 1 2 2 2 3 3 3 4 ...
$ repeat    : Factor w/ 3 levels "1","2","3": 1 2 3 1 2 3 1 2 3 1 ...
$ flighttime: Factor w/ 73 levels "0","3","5","6",..: 73 73 31 57 73 56 73 65 73 73 ...

> boxplot(flighttime-location, xlab="location", ylab="flighttime")

在boxplot中出现了1个框,在这里输入图像描述

添加"x =因素(位置),

> boxplot(flighttime-location, xlab="location", ylab="flighttime")

创建了第二行,在这里输入图像描述

我的目标是创建一个这样的箱线图:在这里输入图像描述

务必键入"~"而不是"-"在x和y变量之间

## Making a reproducible example
location <- c(rep(1:3,length.out=30))
flighttime <- c(sample(5:78,size=30))
ANOVEDATArobin <- data.frame(location, flighttime)

这是你写的,你得到一个大盒子:

boxplot(flighttime - location, xlab="location", ylab="flighttime")

我是这样写的:

boxplot(flighttime ~ location, xlab="location", ylab="flighttime")

甚至,更好的,为什么不玩一下ggplot包!

ggplot(ANOVEDATArobin)+
aes(x = location, y = flighttime, group = location)+
geom_boxplot()

最新更新