ggplot自定义x轴(月)已压缩/不使用整个范围

问题描述 投票:2回答:1

我正在尝试根据时间序列数据创建箱线图。问题在于数据似乎在宽度上“压缩”并且没有覆盖它应该覆盖的范围。我的x轴是观察月份,但按自定义顺序排列(11月至3月)。该图仅涵盖12月至2月,但我绝对有11月和3月的观察结果。

level_order <- c('November', 'December', 'January', 'February', 'March')
plot <- ggplot(data = df, aes(y = y, x = factor(Month,level = level_order), group=DAP)) +
geom_boxplot(fill="grey85", width = 2.0) +
scale_x_discrete(limits = level_order)
plot

结果:X轴范围是正确的,并且所有条目都在那里-但以某种方式在宽度上压缩了...boxplot

这里是数据集的示例

> df
    DAP Date       Month    y
1    47 2010-11-26 November 0.6872708
16   99 2011-01-17  January 0.7929280
31  151 2011-03-10    March 0.6915378
46   85 2012-01-03  January 0.7346495
61  137 2012-02-24 February 0.7178306
76   75 2012-12-24 December 0.7287693
91  127 2013-02-14 February 0.7282626
r ggplot2 boxplot scaling
1个回答
0
投票

您的问题是,我认为width参数不符合您的想法。考虑不使用width参数时发生的情况:

plot <- ggplot(data = df, aes(y = y, x = Month, group = DAP)) +
    geom_boxplot(fill = "grey85") +
    scale_x_discrete(limits = level_order)
plot

enter image description here

这是我想您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.