[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.