当因子有3个水平时,为什么R上的箱形图显示为1个箱形?

ubof19bj  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(141)

我正在尝试对一些数据进行嵌套方差分析测试,并一直遵循R教程。为了可视化数据,我创建了一个箱线图,但当数据中有3个位置时,x轴上只显示了1个框表示"位置"。
所有数据均已使用"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")

在箱形图enter image description here中显示为1个方框
增加"x =系数(位置)"

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

创建了第二条线enter image description here
我的目标是创建如下的箱形图:enter image description here

k97glaaz

k97glaaz1#

确保在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()

相关问题