Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:使用ggplot2绘制带有分位数的时间序列_R_Ggplot2_Time Series - Fatal编程技术网

R:使用ggplot2绘制带有分位数的时间序列

R:使用ggplot2绘制带有分位数的时间序列,r,ggplot2,time-series,R,Ggplot2,Time Series,我需要用ggplot2绘制一个时间序列。对于时间序列的每个点,我也有一些分位数,比如0.05,0.25,0.75,0.95,也就是说,每个点有五个数据。例如: time quantile=0.05 quantile=0.25 quantile=0.5 quantile=0.75 quantile=0.95 00:01 623.0725 630.4353 903.8870 959.1407 1327.721

我需要用ggplot2绘制一个时间序列。对于时间序列的每个点,我也有一些分位数,比如0.05,0.25,0.75,0.95,也就是说,每个点有五个数据。例如:

time           quantile=0.05  quantile=0.25 quantile=0.5  quantile=0.75   quantile=0.95
00:01          623.0725       630.4353      903.8870       959.1407       1327.721
00:02          623.0944       631.3707      911.9967      1337.4564       1518.539
00:03          623.0725       630.4353      903.8870      1170.8316       1431.893
00:04          623.0725       630.4353      903.8870      1336.3212       1431.893
00:05          623.0835       631.3557      905.4220      1079.6623       1452.260
00:06          623.0835       631.3557      905.4220      1079.6623       1452.260
00:07          623.0835       631.3557      905.4220      1079.6623       1452.260
00:08          623.0780       631.3483      905.3496      1056.3719       1375.610
00:09          623.0671       630.4275      903.8839      1170.8196       1356.963
00:10          623.0507       630.0261      741.8475      1006.1208       1462.271
理想情况下,我希望将0.5分位数作为黑线,其他分位数作为围绕黑线的着色颜色间隔。最好的方法是什么?我一直在四处寻找,没有运气,我找不到这样的例子,更不用说ggplot2了

任何帮助都将不胜感激


萨鲁德

假设您的dat.frame被称为
df

最简单的
ggplot
解决方案是使用箱线图几何图形。这将为中间和上部位置提供一条带填充框的黑色中心线

由于您已经预先汇总了数据,因此指定
stat=“identity”
参数非常重要:

ggplot(df, aes(x=time)) + 
    geom_boxplot(
        aes(
          lower=quantile.0.25, 
          upper=quantile.0.75,
          middle=quantile.0.5,
          ymin=quantile.0.05,
          ymax=quantile.0.95
        ), 
        stat="identity",
        fill = "cyan"
)

另外,我重新创建了您的数据,如下所示:

df <- "time           quantile=0.05  quantile=0.25 quantile=0.5  quantile=0.75   quantile=0.95
00:01          623.0725       630.4353      903.8870       959.1407       1327.721
00:02          623.0944       631.3707      911.9967      1337.4564       1518.539
00:03          623.0725       630.4353      903.8870      1170.8316       1431.893
00:04          623.0725       630.4353      903.8870      1336.3212       1431.893
00:05          623.0835       631.3557      905.4220      1079.6623       1452.260
00:06          623.0835       631.3557      905.4220      1079.6623       1452.260
00:07          623.0835       631.3557      905.4220      1079.6623       1452.260
00:08          623.0780       631.3483      905.3496      1056.3719       1375.610
00:09          623.0671       630.4275      903.8839      1170.8196       1356.963
00:10          623.0507       630.0261      741.8475      1006.1208       1462.271"

df <- read.table(textConnection(df), header=TRUE)

df这是你想要的吗?
ggplot
的诀窍在于理解它需要长格式的数据。这通常意味着我们必须在数据准备打印之前对其进行转换,通常使用
melt()

使用
textConnection()
读取中的数据并创建名为
dat
的对象后,以下是您将采取的步骤:

#Melt into long format 
dat.m <- melt(dat, id.vars = "time")

#Not necessary, but if you want different line types depending on quantile, here's how I'd do it
dat.m <- within(dat.m
  , lty <- ifelse(variable == "quantile.0.5", 1
    , ifelse(variable %in% c("quantile.0.25", "quantile.0.75"),2,3)
    )
)

#plot it
ggplot(dat.m, aes(time, value, group = variable, colour = variable, linetype = lty)) + 
  geom_line() +
  scale_colour_manual(name = "", values = c("red", "blue", "black", "blue", "red"))

编辑:强制为预测值创建图例

我们可以使用与
geom_ribbon()
层相同的方法。我们将向
geom\u line()
添加美学,然后使用
scale\u color\u manual()
设置美学的值:


也许有更有效的方法可以做到这一点,但这是我一直使用的方法,并且取得了相当好的成功。YMMV.

给我们一些数据玩。这里有一些关于如何制作一个很好的可复制的例子的指导方针:嗯,我们对他的问题有不同的解释……在重新阅读之后,我不确定谁在正确的道路上!无论如何,展示如何手动创建长方体图的工作做得不错+1是的,我对线条和丝带风格更感兴趣,因为我的系列很长。不过还是要谢谢你,我学到了更多的东西!:)我的答案就像伦敦公共汽车。你可以在看不见任何东西的情况下等三个小时,然后突然在6分钟内你就有了两个!PS.+1几年前卢布尔雅那的公共汽车发出的声音。:)哇,这简直太完美了!非常感谢你!我唯一需要添加的是一个图例,上面写着“预测值”。我怎么能这么做?我可以将“刻度填充”手册与其他刻度填充手册结合使用吗?再次感谢!我尝试添加
scale\u linetype\u manual(name=”,values=c(“predicted”=1))
但没有效果…@inwit-现在将使用一个选项更新我的答案
ggplot(dat, aes(x = time, group = 1)) +
  geom_ribbon(aes(ymin = quantile.0.05, ymax = quantile.0.95, fill = "05%-95%"), alpha = .25) + 
  geom_ribbon(aes(ymin = quantile.0.25, ymax = quantile.0.75, fill = "25%-75%"), alpha = .25) +
  geom_line(aes(y = quantile.0.5)) +
  scale_fill_manual(name = "", values = c("25%-75%" = "red", "05%-95%" = "blue")) 
ggplot(dat, aes(x = time, group = 1)) +
  geom_ribbon(aes(ymin = quantile.0.05, ymax = quantile.0.95, fill = "05%-95%"), alpha = .25) + 
  geom_ribbon(aes(ymin = quantile.0.25, ymax = quantile.0.75, fill = "25%-75%"), alpha = .25) +
  geom_line(aes(y = quantile.0.5, colour = "Predicted")) +
  scale_fill_manual(name = "", values = c("25%-75%" = "red", "05%-95%" = "blue")) +
  scale_colour_manual(name = "", values = c("Predicted" = "black"))