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)
Related
I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot
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 add multiple vertical lines to a time series plot.
Normally you would specify abline(v=x-intercept) but my x-axis is in the form 01/08/2000 – 31/07/2001. How would I adapt the abline code to add vertical lines for example 13/10/2000 and 30/05/2001?
Firstly, I have tried the following zoo plot but the x-axis is not giving me the preferred date format;
dat<-read.table("Aug2000-July2001.txt", sep="\t", header=T)
z <- with(dat, zoo(cbind(NEE,SWC)))
options("na.actions"=na.omit)
plot.zoo(z, ylab=c("NEE(umolm-2s-1)", "SWC(%)"),
col=c("black","blue"), xlab=c("Date"), lwd=2)
Secondly, I have also tried e.g.
abline(v=as.Date("13/10/2000"))
but it is not working. How do I add multiple vertical lines and how do I add my date format on the x-axis on a zoo plot?
I have a table with some of the columns as follows:
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.
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() ?