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

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

问题描述

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

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

理想情况下,我希望将 0.5 分位数作为黑线,将其他分位数作为围绕黑线的阴影颜色间隔.最好的方法是什么?我一直在四处寻找没有运气,我找不到这样的例子,更不用说 ggplot2.

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.

任何帮助将不胜感激.

祝你好运!

推荐答案

这是你想要的吗?ggplot 的诀窍是理解它需要长格式的数据.这通常意味着我们必须在准备绘图之前转换数据,通常使用 melt().

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().

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

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

给你:

再次阅读您的问题后,也许您想要中位数估计之外的阴影色带而不是线条?如果是这样,试一试.这里唯一真正的技巧是我们将 group = 1 作为美学传递,以便 geom_line() 可以正确处理因子/字符数据.以前,我们按具有相同效果的变量进行分组.另请注意,我们不再使用 melted data.frame,因为在这种情况下,宽 data.frame 将非常适合我们.

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

强制为预测值添加图例

我们可以使用与 geom_ribbon() 层相同的方法.我们将向 geom_line() 添加美学,然后使用 scale_colour_manual() 设置该美学的值:

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

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

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天全站免登陆