我正在尝试生成一个双分组并排箱线图,每个机场(始发地)都有两个箱线图,y 轴是飞行时间。
我的数据框是 sba_connections_boxplot,如下所示:
这是我生成双分组并排箱线图的代码:
ggplot(sba_connections_boxplot, aes(x = ORIGIN,
y = ACTUAL_ELAPSED_TIME,
fill = Direction)) +
geom_boxplot(staplewidth = 0.5) +
theme_minimal(base_size = 12) +
ggtitle(
"Distribution of Flight Durations for SBA Flights",
subtitle = "Grouped by airport and direction"
)+
theme(
axis.text.x = element_text(angle = 45)
)+
ylim(-100, 500) +
labs(x = "airport", y = "Flight duration")
显然,在我的图表中,每个机场只有一种方向类型,但它们应该有两种。此外,我的箱线图垂直缩小了。
请指出我如何修复我的箱线图,使其看起来与理想输出一样漂亮。谢谢
像这样(使用一些虚构的数据)与
position = position_dodge()
?
library(ggplot2)
library(tibble)
# toy data
df <- tibble(
elapsed = rnorm(100, 150, 50),
origin = rep(c("DFW", "PHX", "SBA", "SEA", "LAX", "PDX", "SBA", "PHX", "SFO", "SMF"), 10),
direction = sample(c("destination", "departure"), 100, replace = TRUE)
)
df |>
ggplot(aes(origin, elapsed, fill = direction)) +
geom_boxplot(staplewidth = 0.5, position = position_dodge()) +
theme_minimal(base_size = 12) +
theme(axis.text.x = element_text(angle = 45)) +
labs(
title = "Distribution of Flight Durations for SBA Flights",
subtitle = "Grouped by airport and direction",
x = "airport", y = "Flight duration"
)
创建于 2024-04-20,使用 reprex v2.1.0