Richard
Richard

Reputation: 61289

Remove back-linking lines in periodic data

I am plotting a periodic function, but using a floating-point modulus to overlay all of the periods onto a single a plot:

library(ggplot2)
x<-seq(1,100,by=0.2)                   #Generate high-res time series
y<-sin(x)                              #Generate y-values
y<-y+rnorm(length(y),mean=0,sd=0.1)    #Add noise
dat<-data.frame(x,y)                   #Package everything

ggplot(dat,aes(x=x,y=y))+geom_line()   #Data looks appropriate

dat$cyc<-dat$x%%(2*pi)                 #Reduce time to time-in-period

plot(dat$cyc, dat$y, type="l")         #This plot is... problematic

ggplot(dat,aes(x=cyc,y=y))+geom_line() #Why isn't this plot problematic?

The raw data looks like this:

A noisy sine wave

I want to overlay all of the periods on top of each other, like this:

Periods overlaid, but with bothersome lines connecting the beginning and end of periods

BUT the end of one period connects with the other, criss-crossing the figure with problematic lines! How can I get rid of these?

The same figure plotted with ggplot does not show the problem, why is that?

Periods overlaid using ggplot

Upvotes: 0

Views: 79

Answers (1)

David Robinson
David Robinson

Reputation: 78610

The reason that plot added extra lines is that it was plotting the points in the order you gave it. Two ways to fix this are to use a loop that occurs once for each period to do the plotting, or to insert NAs into the data between each period. For example, you could insert NAs with this plyr code:

library(plyr)
dat$iteration <- floor(dat$x / (2 * pi))
dat <- plyr::ddply(dat, "iteration", function(d) rbind(d, NA))

Or this dplyr code:

library(dplyr)
dat <- dat %>%
    mutate(iteration = floor(x / (2 * pi))) %>%
    group_by(iteration) %>%
    do(rbind(., NA))

At that point you could plot using plot(dat$cyc, dat$y, type="l"). However, you're really better off using ggplot2. (Is there a reason you want to use base plotting?)

The reason your ggplot2 looks better is that it automatically rearranges the points in order of the x-axis. But ggplot2 still has its own problem, which is that it's plotting all the periods as though they're the same data- it's not plotting them as a separate curve for each period (notice that it doesn't look the same as the plot results, even aside from the horizontal lines). To do that, you'll have to add a group aesthetic:

dat$iteration <- floor(dat$x / (2 * pi))
ggplot(dat, aes(x = cyc, y = y, group = iteration)) + geom_line()

Upvotes: 1

Related Questions