Plotting xts objects works with points but not with lines

跟風遠走 提交于 2019-12-11 04:34:59

问题


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.

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)

回答1:


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.



来源:https://stackoverflow.com/questions/18858215/plotting-xts-objects-works-with-points-but-not-with-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!