R: why is plot of time series much cleaner than original - r

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

Related

Plot 2 xts time series in one plot

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

Plotting xts objects works with points but not with lines

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.

Plotting discontinuous xts timeseries on same plot in R?

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)

Plotting distribution of differences in R

I have a dataset with numbers indicating daily difference in some measure.
https://dl.dropbox.com/u/22681355/diff.csv
I would like to create a plot of the distribution of the differences with special emphasis on the rare large changes.
I tried plotting each column using the hist() function but it doesn't really provide a detailed picture of the data.
For example plotting the first column of the dataset produces the following plot:
https://dl.dropbox.com/u/22681355/Rplot.pdf
My problem is that this gives very little detail to the infrequent large deviations.
What is the easiest way to do this?
Also any suggestions on how to summarize this data in a table? For example besides showing the min, max and mean values, would you look at quantiles? Any other ideas?
You could use boxplots to visualize the distribution of the data:
sdiff <- read.csv("https://dl.dropbox.com/u/22681355/diff.csv")
boxplot(sdiff[,-1])
Outliers are printed as circles.
I back #Sven's suggestion for identifying outliers, but you can get more refinement in your histograms by specifying a denser set of breakpoints than what hist chooses by default.
d <- read.csv('https://dl.dropbox.com/u/22681355/diff.csv', header=TRUE, row.names=1)
with(d, hist(a, breaks=seq(min(a), max(a), length.out=100)))
Violin plots could be useful:
df <- read.csv('https://dl.dropbox.com/u/22681355/diff.csv')
library(vioplot)
with(df,vioplot(a,b,c,d,e,f,g,h,i,j))
I would use a boxplot on transformed data, e.g.:
boxplot(df[,-1]/sqrt(abs(df[,-1])))
Obviously a histogram would also look better after transformation.

In R, how to plot bars only in a certain interval of data?

My problem is very simple.
I have to plot a data series in R, using bars. Data are contained in a vector vet.
I've used barplot, that plots my data from the first to the last:
barplot(vet), and everything was fine.
Now, on the contrary, I would like to plot not all my data, but just a part of them: from 10% to the end.
How could I do this with barplot()?
How could I do this with plot()?
Thanx
You need to subset your data before plotting:
##Work out the 10% quantile and subset
v = vet[vet > quantile(vet, 0.1)]
It is not clear exactly what you want to do.
If you want to plot only a subset of the bars (but the whole bars) then you could just subset the data before passing it to barplot.
If you want to plot all the bars, but only that part beyond 10% (not include 0) then you can do this by setting the ylim argument. But it is very discouraged to do a barplot that does not include 0. You may be better off using a dotplot instead of a barplot if 0 is not meaningful.
If you want the regular plot, but want to exclude plotting outside of a given window within the plot then the clip function may be what you want.
The gap.barplot function from the plotrix package may also be what you want.

Resources