绘制 xts 对象适用于点但不适用于线 [英] Plotting xts objects works with points but not with lines

查看:56
本文介绍了绘制 xts 对象适用于点但不适用于线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经合并了两个 xts 对象,并想在一个显示中绘制它们.当我使用点(type =p")时,这很好用.但是,当我使用行 (type="l") 时,会出现问题:第一个系列仅显示在第二个系列未涵盖的索引区域中.我希望线条与点"一样长.下面发布了一个可重现的示例.

I have merged two xts objects and want to plot them in a single display. This works fine when I use points (type="p"). However, when I use lines (type="l") a problem occurs: the first series is shown only in the index region that is not covered by the second series. I would expect the lines to be as long as the "points". A reproducible example is posted below.

由于默认和 ggplot 绘图命令都会发生这种情况,我怀疑这与时间序列数据的某些属性有关.

As this occurs with both the default and the ggplot plotting commands, I suspect that this relates to some property of time-series data.

这种行为的原因是什么?有没有合适的方法来绘制这种数据?

What is the reason for this behaviour? Is there a proper way of plotting this kind of data?

## Minimal example for Reproduction
library(xts)
library(ggplot)
# create two artificial xts objects
xts1 <- xts(1:15,Sys.Date()+10+seq(from=1,by=5,length.out=15))
xts2 <- xts(1:20,Sys.Date()+seq(from=1,by=2,length.out=20))

# merge them
merged.xts <- merge.xts(xts1,xts2)

# Plot as zoo objects to allow for panels
# plotting with points shows both series
plot(as.zoo(merged.xts),type="p",plot.type="single")

# plotting with lines
# The second series is "shortened"
plot(as.zoo(merged.xts),type="l",plot.type="single")

# Similar behaviour with ggplot2
autoplot(merged.xts)

推荐答案

很简单,type="l" 看起来就是这样,因为您无法在单个点上绘制一条线.设置 type="b" 以查看 b条线和点.

Quite simply, type="l" looks the way it does because you can't plot a line on a single point. Set type="b" to see both lines and points.

这篇关于绘制 xts 对象适用于点但不适用于线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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