【问题标题】:How can I make a time-series boxplot in ggplot2?如何在 ggplot2 中制作时间序列箱线图?
【发布时间】:2016-05-02 05:21:42
【问题描述】:

我正在做贝叶斯分析。我有 100 家商店的月度后验销售数据。我想使用可视化这些数据的 ggplot 创建一个箱线图。我面临的问题是我不确定如何让 ggplot 知道有 100 列和 12 行。

这里是数据示例。这是一个link to the data,它非常小,13 行月份和 100 列代表 100 家商店。第 13 行是手段。

> head(t.data)
               1          2         3         4          5           6          7          8          9        10
Month1 -26.25917   1.740833 151.74083 67.740833  21.740833  -42.259167  -1.259167  49.740833  -53.25917 -98.25917
Month2 -27.25917  -9.259167 117.74083 74.740833  -2.259167    5.740833  64.740833  59.740833  -41.25917 -59.25917
Month3 -32.25917 -68.259167  42.74083 79.740833 -61.259167 -113.259167  26.740833 -36.259167  -81.25917 -32.25917
Month4 -37.25917  -9.259167 128.74083 -6.259167 -14.259167  -38.259167  69.740833  28.740833  -46.25917 -16.25917
Month5  15.74083   5.740833 102.74083 89.740833 -57.259167  -60.259167 112.740833 -28.259167  -42.25917 -41.25917
Month6 -22.25917  -9.259167  82.74083 11.740833 -42.259167  -56.259167  42.740833   4.740833 -101.25917 -58.25917
               11        12        13         14        15        16         17        18          19         20
Month1   47.74083 160.74083 -63.25917   8.740833  58.74083  20.74083  64.740833 -13.25917 -35.2591667  -78.25917
Month2   38.74083 153.74083 -36.25917 -57.259167  46.74083 -71.25917  22.740833  23.74083 -29.2591667 -108.25917
Month3   37.74083  77.74083 -29.25917 -49.259167 127.74083 -63.25917 -18.259167 -34.25917 -98.2591667 -143.25917
Month4   49.74083 189.74083 -46.25917 -54.259167  97.74083 -27.25917  55.740833 -43.25917 -54.2591667  -82.25917
Month5  -14.25917  78.74083  18.74083 -16.259167  47.74083 -13.25917  19.740833 -22.25917   0.7408333  -91.25917
Month6 -164.25917  83.74083  10.74083 -22.259167 -14.25917 -33.25917  -4.259167 -16.25917 -34.2591667 -105.25917

那么基本上,我将如何使用这种类型(时间序列)的数据复制 base R 的箱线图函数?这是标准 R 箱线图的可重现示例。 Here is a link 到 q.csv 数据。

t.data <- read.csv("t.data.csv", header=TRUE)  
q <- read.csv("q.csv", header=TRUE)
colnames(t.data) <- paste("", 1:100, sep = "")  
boxplot(t.data,xlab="Store Number",ylab="Effect of Promo on Sales (centered)",outline=FALSE,
            main="Data versus posterior of the spaial random effect")

lines(q[1:100,1],col=2,lty=2)
lines(q[1:100,2],col=2,lty=1)
lines(q[1:100,3],col=2,lty=2)

legend("topright",c("Median","95% interval"),lty=1:2,col=2,bg=gray(1),inset=0.05)

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    我们需要先融化t.data,然后才能使用ggplot2

    library(ggplot2)
    ggplot(melt(t.data), aes(variable, value)) + 
      geom_boxplot()
    

    【讨论】:

    • 行到底是什么?
    • 中位数和 95% 区间线,在示例中显示为lines(q[1:100,1],col=2,lty=2)lines(q[1:100,2] ,col=2,lty=1) 行(q[1:100,3],col=2,lty=2)
    • 抱歉,我已经添加了 q 数据的链接和加载它的行。
    • 没关系,我想通了!感谢您的帮助!
    猜你喜欢
    • 2014-04-18
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    相关资源
    最近更新 更多