我正在尝试使用ggplot2连接箱形图的中值,但是这些线的位置不正确。
这是我使用的代码。
library(datasets)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality$Day <- ifelse(airquality$Day >= 15, 'End', 'Begining')
ggplot(airquality, aes(x = Month, y = Ozone, fill = Day)) +
geom_boxplot() +
stat_summary(fun.y = median, geom = 'line', aes(group = Day, colour =Day))
任何帮助将不胜感激。
答案 0 :(得分:0)
为此,您应显式指定位置参数。 geom_boxplot
默认为position_dodge
,但是您必须为该行指定它。
ggplot(airquality, aes(x = Month, y = Ozone, fill = Day)) +
geom_boxplot(position = position_dodge(width = 0.9)) +
stat_summary(
fun.y = median,
geom = 'line',
aes(group = Day, colour = Day),
position = position_dodge(width = 0.9) #this has to be added
)