侧面箱线图标签的比较

3

这是我的数据集 dput_data

dput_data <- structure(list(Label = c(" Flight", " Flight", " Flight", " Flight", 
" Flight", " Ground Control", " Ground Control", " Ground Control", 
" Ground Control", " Ground Control", " Ground Control"), `210885` = c(2.46726120893655, 
-0.174636990542105, -0.463404328357544, 2.3996395908089, -0.150507030748742, 
5.64497005685753, 3.36669883823842, 0.0291245138878325, -0.0720040453347311, 
1.23592496897254, -0.435680321628551), `110647` = c(-0.183451995597915, 
-0.232101174582698, -0.418549509261665, 3.12474114229781, -0.963627404680163, 
-0.295591624345765, 3.74110668642539, -0.307620588051106, 4.95070981495709, 
-0.248418667713625, 0.556071497195402), `120996` = c(2.958740197185, 
-0.658764097795927, -0.720985892268865, -0.605140415143121, -0.614856607147667, 
2.84170000321244, 2.4703391289031, 1.89042697528755, 1.38117056072924, 
2.66548725562505, -0.775258323014181)), class = "data.frame", row.names = c(35L, 
36L, 37L, 38L, 39L, 44L, 45L, 46L, 47L, 48L, 49L))

看起来像这样

             Label      210885     110647     120996
35          Flight  2.46726121 -0.1834520  2.9587402
36          Flight -0.17463699 -0.2321012 -0.6587641
37          Flight -0.46340433 -0.4185495 -0.7209859
38          Flight  2.39963959  3.1247411 -0.6051404
39          Flight -0.15050703 -0.9636274 -0.6148566
44  Ground Control  5.64497006 -0.2955916  2.8417000
45  Ground Control  3.36669884  3.7411067  2.4703391
46  Ground Control  0.02912451 -0.3076206  1.8904270
47  Ground Control -0.07200405  4.9507098  1.3811706
48  Ground Control  1.23592497 -0.2484187  2.6654873
49  Ground Control -0.43568032  0.5560715 -0.7752583

目前我已经尝试过:

library(reshape2)
dput_dat <- melt(dput_data, id.vars = 'Label', measure.vars = c('210885', '110647', '120996'))
library(ggplot2)
ggplot(dput_dat) + geom_boxplot(aes(y=value, color=variable)) + facet_grid(.~`Label`)

这给我两组箱线图,一组是航班数据,另一组是地面控制数据。 enter image description here 但我更希望将210885的航班和地面控制箱线图并排放置,以便进行直接的对比(航班和地面控制使用不同颜色)。110647120996也是如此。所有这些都应该在一个图表中。
我有点不知道该怎么做。
1个回答

2
使用标签作为x变量,将您的变量用于分面,如下所示:
library(tidyverse)

data %>% pivot_longer(-Label, names_to = "type", values_to = "value") %>%
  arrange(desc(type)) %>%
  mutate(type = factor(type, levels = unique(type))) %>%
  ggplot(aes(x = Label, y = value, color = Label))+
  geom_boxplot()+
  facet_wrap(~type)

这是您要找的吗?

这是您要找的吗?


(注:该文本为中英文混合内容,下同)

哦,我希望飞行和地面控制的颜色有所区别。但这仍然是可以接受的。您知道我该如何订购图表(从左到右 - 210885、110647、120996)吗? - strkiky2
不用谢 ;). 我已经编辑了我的答案,向你展示了如何设置标签的颜色,并设置了所需的分面顺序。如果对你没问题的话,请告诉我。 - dc37
哦,谢谢!我注意到你使用了降序排列,我能否自己指定顺序? - strkiky2
抱歉,我没有注意到顺序不是降序的。您可以删除排列并通过 mutate(type = factor(type, levels = c("210885", "110647", "120996"))) 更改 mutate 函数,这应该可以解决问题。 - dc37

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接