简体   繁体   中英

How to plot a time series boxplot from summary statistics?

I was replicating a graph. I don't have the original data, so I use the bxp command. That way I could graph the boxplot for each year individually with summary statistics, however, I would like the plots that I made to be in a single plot as if it were a time series. I would like you to give me an advice or an idea where to start. Thanks in advance.

This is my code:

# 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)

This is my desired output:

在此处输入图片说明

In order to plot all box plots at once, you need to construct the right kind of list:

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

Then, it can simply be plotted via

bxp(z)

在此处输入图片说明

EDIT
The following creates the plot with the y axis to the right and the correct x axis labels.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM