【问题标题】:How to plot a time series boxplot from summary statistics?如何从汇总统计中绘制时间序列箱线图?
【发布时间】:2021-01-03 23:02:31
【问题描述】:

我正在复制一个图表。我没有原始数据,所以我使用 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)

这是我想要的输出:

【问题讨论】:

    标签: r plot time-series boxplot


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢。它真的很有用。以及如何在 x 轴上添加年份和“长期运行”?另外我如何将 y 轴向左移动并将 y 轴值扩展到 -6 到 6?
    • @JoseMontoya 很高兴能帮上忙!请参阅带有您请求的更改的新绘图的编辑。
    猜你喜欢
    • 2011-07-10
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多