具有自定义X轴的ggplot箱线图,并对单独的值进行分组和排序

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

我正在尝试根据多年的时间序列数据创建箱线图。我想通过变量“ DAP”(类似于0-365年的一天)对多年的观测进行分组,从11月到3月按天对它们进行排序,但仅在X轴上显示月份。

我可以通过创建每个月有效的因子来创建自定义订单和X轴,>

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

![enter image description here

现在,我根据每个月的天数在X轴上进行对齐。例如,从11月26日开始的第一个数据点需要更正确,更接近12月。

将X轴更改为“日期”会为每年创建每月标签,并删除分组。

plot <- ggplot(data = df, aes(y = y, x = Date, group=DAP)) +
  geom_boxplot(fill="grey85")
plot + scale_x_date(date_breaks = "1 month", date_labels = "%B")

enter image description here

将X轴设置为“ DAP”而不是日期会给我正确的顺序和间距,但是我需要在X轴上显示月份。如何将最后一个图形与图形1的X轴标签结合在一起?

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

enter image description here

这里是数据集的示例

    DAP Date       Month    y
1    47 2010-11-26 November 0.6872708
21  116 2011-02-03 February 0.7643213
41   68 2011-12-17 December 0.7021531
61  137 2012-02-24 February 0.7178306
81   92 2013-01-10  January 0.7330749
101  44 2013-11-23 November 0.6610618
121 113 2014-01-31  January 0.7961012
141  68 2014-12-17 December 0.7510821
161 137 2015-02-24 February 0.7799938
181  92 2016-01-10  January 0.6861423
201  47 2016-11-26 November 0.7155526
221 116 2017-02-03 February 0.7397810
241  72 2017-12-21 December 0.7259670
261 144 2018-03-03    March 0.6725775
281 106 2019-01-24  January 0.7637322
301  65 2019-12-14 December 0.7184616
321 134 2020-02-21 February 0.6760159
    

我正在尝试根据多年的时间序列数据创建箱线图。我想通过变量“ DAP”(类似于0-365年的一天)对多年的观察结果进行分组,并按...

r ggplot2 boxplot axis-labels
2个回答
0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.