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)
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.
Related
I tried plotting original data, say dt, and compared the plot obtained by making it into a ts object, i.e.,
plot(dt)
versus
plot(ts(dt))
The first plot is very hard to visualize, while second one looks great. I then tried to see result of
ts(dt)-dt
The result was all 0s, so apparently R did not manipulate data points.
In base R, plot.ts makes a line graph, while plot of a vector makes a point graph. That's the only difference.
plot(1:10)
plot(ts(1:10))
I want to combine a time series of in situ values (line) with boxplots of estimated values of special dates. I tried to understand this "Add a line from different result to boxplot graph in ggplot2" question, but my dates make me drive crazy. Sometimes I only have in situ values of a date, sometimes only estimated values and sometimes both together.
I uploaded a sample of my data here:
http://www.file-upload.net/download-9942494/estimated.txt.html
http://www.file-upload.net/download-9942495/insitu.txt.html
How can I create a plot with both data sets that looks like this http://www.file-upload.net/download-9942496/desired_outputplot.png.html
in the end?
I got help and have a solution now:
insitu <- read.table("insitu.txt",header=TRUE,colClasses=c("Date","numeric"))
est <- read.table("estimated.txt",header=TRUE,colClasses=c("Date","numeric"))
insitu.plot <- xyplot(insitu~date_fname,data=insitu,type="l",
panel=function(x,y,...){panel.grid(); panel.xyplot(x,y,...)},xlab=list(label="Date",cex=2))
est.plot <- xyplot(estimated~date,data=est,panel=panel.bwplot,horizontal=FALSE)
both <- insitu.plot+est.plot
update(both,xlim=range(c(est$date,insitu$date_fname))+c(-1,1),ylim=range(c(est$estimated,insitu$insitu)))
Hi I thought this would be a simple task, 2hrs later and I am still struggling.
I was able with this code
chartSeries(snp.obj, TA=c("addTA(over,layout=NULL)"))
However it comes with a 2 paned plot but I am looking for these two xts objects overlayed with different y-axis to be in one plot not like I have it in the chartSeries plot.
Answer that works but possibly not so elegant:
over = xts(over, order.by=snp.obj[121:1730])
plot(snp.obj, main='Shiller PE Timer')
lines(2000*over+1, col= 'red')`
Use the on= argument to addTA (see ?addTA)
chartSeries(snp.obj, TA=c("addTA(over, on=1)"))
I have multiple time series objects covering different periods of time, with gaps in the data, and with varying frequencies (some hourly data, some 15-minute, some 1-minute).
I'm trying to plot different time series objects against one another in x-y scatterplots - to see if there are obvious correlations, and make 'pretty' plots with ggplot for presentation. Obviously, one can only plot/compare data where it exists for x and y at the same time.
I'm able to get a quick graphic with base graphics, but would like something more presentable.
for example, this works in base R:
require(zoo)
x <- zoo(sort(rnorm(10)),as.POSIXct("2013/07/26")+1:10)
y <- zoo(sort(rnorm(30)),as.POSIXct("2013/07/26")+(1:30)/2)
plot(x,y)
and trying to do the same with ggplot fails:
data <- rbind(melt(fortify(x),id="Index"),melt(fortify(y),id="Index"))
ggplot(data,aes(x=x,y=y))+geom_point()
Don't know how to automatically pick scale for object of type zoo. Defaulting to continuous
Don't know how to automatically pick scale for object of type zoo. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:x
suggestions on better titles/description are welcome
What about this:
aaa<-merge(x,y, all=FALSE)
ggplot(aaa,aes(x=x,y=y))+geom_point() ?
I'd like to be able to do a line plot of several discontinuous time series on the same screen in R, and am having trouble getting all data to appear at once:
require(xts)
require(xtsExtra)
df1=data.frame(a=1:30,b=3*1:30)
df1$b[2*1:15]=NA
df1A_xts=xts(df1,ISOdate(1900+1:30,1,1))
df1B_xts=xts(df1,ISOdate(1900+2*1:30,2,1))
df1_xts_merge=merge.xts(df1A_xts,df1B_xts)
Of course, when I plot as a point graph, everything shows up okay:
plot.xts(df1_xts_merge,screens=1,type="p",auto.legend=TRUE)
But when I try to plot one or more series as lines, the discontinuities cause trouble, e.g.:
plot.xts(df1_xts_merge,screens=1,auto.legend=TRUE)
How can I plot each of these series as a continuous line on the same set of axes?
xts and zoo behave consistent with the default plotting methods:
matplot(df1_xts_merge,type="l")
You seem to want a plot of the lines interpolated through the points in your xts object, in which case na.approx or na.spline will be helpful:
plot(na.approx(df1_xts_merge),screens=1,auto.legend=TRUE)