R:用ggplot2绘制分位数的时间序列 [英] R: Plot a time series with quantiles using ggplot2

查看:486
本文介绍了R:用ggplot2绘制分位数的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 时间分位数= 0.05分位数= 0.25分位数= 0.5分位数= 0.75分位数= 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 1 462.271

理想情况下,我希望将0.5分位数作为黑线,其他作为阴影颜色黑线周围的间隔。什么是最好的方法来做到这一点?我一直在四处寻找,我找不到这样的例子,甚至更少使用ggplot2。



任何帮助将不胜感激。



Salud!

解决方案

这是做你想做的吗?把 ggplot 的技巧理解为它需要长格式的数据。这通常意味着我们必须在准备绘制数据之前转换数据,通常使用 melt()



使用 textConnection()读取数据并创建一个名为 dat 的对象后,您可以按照以下步骤操作采取:

 #融入长格式
dat.m< - melt(dat,id.vars =时间)

#不需要,但是如果你想要根据分位数得到不同的线型,这里是我该怎么做
dat.m< - within(dat.m
,lty < - ifelse(变量==quantile.0.5,1
,ifelse(变量%in%c(quantile.0.25,quantile.0.75),2,3)



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

给您:



再次阅读您的问题后,也许您想要在中值估算之外的阴影色带而不是线条?如果是这样,给这个旋转。这里唯一真正的技巧就是我们传递 group = 1 作为美学,这样 geom_line()因子/字符数据。以前,我们通过服务于相同效果的变量进行分组。另外请注意,我们不再使用 melt ed data.frame,因为在这种情况下宽数据框架适合我们。

  ggplot(dat,aes(x = time,group = 1))+ 
geom_ribbon(aes(ymin = quantile.0.05,ymax = quantile (amin(ymin = quantile.0.25,ymax = quantile.0.75,fill =25%-75%)。),则填充=05%-95%),alpha = .25)+
geom_ribbon ,alpha = .25)+
geom_line(aes(y = quantile.0.5))+
scale_fill_manual(name =,values = c(25%-75%=red, 05%-95%=blue))



编辑:强制预测值的图例



我们可以使用我们用于 geom_ribbon()图层的相同方法。我们将为 geom_line()添加审美元素,然后使用 scale_colour_manual()设置审美的值:

  ggplot(dat,aes(x = time,group = 1))+ 
geom_ribbon(aes(ymin = quantile .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,color =Predicted))+
scale_fill_manual(name =,values = c 25%-75%=红色,05%-95%=蓝色))+
scale_colour_manual(name =,values = c(Predicted=black))

可能有更有效的方法可以做到这一点,但这是我一直使用的方式它取得了相当不错的成功。 YMMV。


I need to plot a time series with ggplot2. For each point of the time series I also have some quantiles, say 0.05, 0.25, 0.75, 0.95, i.e. I have five data for each point. For example:

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

Ideally, I would like to have the 0.5 quantile as a black line and the others as shaded color intervals surrounding the black line. What's the best way to do this? I've been looking around with no luck, I can't find examples of this, even less with ggplot2.

Any help would be appreciated.

Salud!

解决方案

Does this do what you want? The trick to ggplot is understanding that it expects data in long format. This often means that we have to transform the data before it is ready to plot, usually with melt().

After reading your data in with textConnection() and creating an object named dat, here are the steps you'd take:

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

Gives you:

After reading your question again, maybe you want shaded ribbons outside the median estimate instead of lines? If so, give this a whirl. The only real trick here is that we pass group = 1 as an aesthetic so that geom_line() will behave properly with factor / character data. Previously, we grouped by the variable which served the same effect. Also note that we are no longer using the melted data.frame, as the wide data.frame will suit us just fine in this case.

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

Edit: To force a legend for the predicted value

We can use the same approach we used for the geom_ribbon() layers. We'll add an aesthetic to geom_line() and then set the values of that aesthetic with scale_colour_manual():

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

There may be more efficient ways to do that, but that's the way I've always used and have had pretty good success with it. YMMV.

这篇关于R:用ggplot2绘制分位数的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆