如何从汇总统计中绘制时间序列箱线图?

How to plot a time series boxplot from summary statistics?

我正在复制一个图表。我没有原始数据,所以我使用 bxp 命令。这样我就可以用汇总统计数据单独绘制每年的箱线图,但是,我希望我制作的图在一个图中,就好像它是一个时间序列一样。我希望你给我一个建议或想法从哪里开始。提前致谢。

这是我的代码:

# Data and plots ----------------------------------------------------------


# Summary data and boxplot 2020 -------------------------------------------

summarydata2020<-list(stats=matrix(c(-5.5,-4,-3.7,-3,1)), n=10)

bxp(summarydata2020, whisklty = 1, range = 0 , staplelwd = 3,staplecol = "palegreen 3", staplewex = 1.1,  boxfill = "palegreen 2", outpch = 8, outlty = 2,
    bg = "pink", lwd = 2, outcex = 3 ,
    medcol = "red", medcex = 2, medpch = 20 , whiskcol = "palegreen 3" , border = c("palegreen 3"), whisklwd = 3 , boxlwd = 3)

# Summary data and boxplot 2021 -------------------------------------------

summarydata2021<-list(stats=matrix(c(0,3.6,4,4.7,5.5)), n=10)

bxp(summarydata2021, whisklty = 1, range = 0 , staplelwd = 3,staplecol = "palegreen 3", staplewex = 1.1,  boxfill = "palegreen 2", outpch = 8, outlty = 2,
    bg = "pink", lwd = 2, outcex = 3 ,
    medcol = "red", medcex = 2, medpch = 20 , whiskcol = "palegreen 3" , border = c("palegreen 3"), whisklwd = 3 , boxlwd = 3)

# Summary data and boxplot 2022 -------------------------------------------


 summarydata2022<-list(stats=matrix(c(2,2.5,3,3.3,4.5)), n=10)

bxp(summarydata2022, whisklty = 1, range = 0 , staplelwd = 3,staplecol = "palegreen 3", staplewex = 1.1,  boxfill = "palegreen 2", outpch = 8, outlty = 2,
    bg = "pink", lwd = 2, outcex = 3 ,
    medcol = "red", medcex = 2, medpch = 20 , whiskcol = "palegreen 3" , border = c("palegreen 3"), whisklwd = 3 , boxlwd = 3)


# Summary data and boxplot 2023 -------------------------------------------
summarydata2023<-list(stats=matrix(c(2,2.4,2.5,3,4)), n=10)

bxp(summarydata2023, whisklty = 1, range = 0 , staplelwd = 3,staplecol = "palegreen 3", staplewex = 1.1,  boxfill = "palegreen 2", outpch = 8, outlty = 2,
    bg = "pink", lwd = 2, outcex = 3 ,
    medcol = "red", medcex = 2, medpch = 20 , whiskcol = "palegreen 3" , border = c("palegreen 3"), whisklwd = 3 , boxlwd = 3)

# Summary data and boxplot Long run -------------------------------------------

summarydataLR <-list(stats=matrix(c(1.6,1.7,1.9,2,2.2)), n=10)

bxp(summarydataLR, whisklty = 1, range = 0 , staplelwd = 3,staplecol = "palegreen 3", staplewex = 1.1,  boxfill = "palegreen 2", outpch = 8, outlty = 2,
    bg = "pink", lwd = 2, outcex = 3 ,
    medcol = "red", medcex = 2, medpch = 20 , whiskcol = "palegreen 3" , border = c("palegreen 3"), whisklwd = 3 , boxlwd = 3)

这是我想要的输出:

为了一次绘制所有箱线图,您需要构建正确类型的列表:

z <- list(stats = cbind(summarydata2020$stats, summarydata2021$stats, summarydata2022$stats, summarydata2023$stats, summarydataLR$stats),
          n = c(summarydata2020$n, summarydata2021$n, summarydata2022$n, summarydata2023$n, summarydataLR$n))


# $stats
#      [,1] [,2] [,3] [,4] [,5]
# [1,] -5.5  0.0  2.0  2.0  1.6
# [2,] -4.0  3.6  2.5  2.4  1.7
# [3,] -3.7  4.0  3.0  2.5  1.9
# [4,] -3.0  4.7  3.3  3.0  2.0
# [5,]  1.0  5.5  4.5  4.0  2.2
# 
# $n
# [1] 10 10 10 10 10

然后,可以简单地通过

绘制
bxp(z)

编辑
下面创建了 y 轴在右边和正确的 x 轴标签的图。

bxp(z, show.names = FALSE, ylim = c(-6,6), yaxt = "n") # do not label axes
axis(1, at = 1:5, labels = c("2020", "2021", "2022", "2023", "Longer run")) # add x axis labels
axis(4, at = -6:6) # add y axis to the right