device_xbreaks<-function(){
usr_x_range<-par("usr")[1:2] #x
usr_x_pixel_range<-floor(grconvertX(usr_x_range, from="user", to="device"))
usr_x_pixels <- usr_x_pixel_range[1] : usr_x_pixel_range[2]
return( grconvertX(usr_x_pixels + .0, from="device", to="user") )
}
compress<-function(xy){
breaks<-device_xbreaks()
cuts = findInterval(xy$x,breaks);
suppressMessages(library(data.table))
dt = data.table(x=xy$x,y=xy$y,cuts=cuts)
dmax = dt[,list(x=max(x),y=max(y)),keyby="cuts"]
dmax$cuts=NULL;
dmin = dt[,list(x=min(x),y=min(y)),keyby="cuts"]
dmin$cuts=NULL;
n=nrow(dmin)
s <- rep(1:n, each = 2) + (0:1) * n
return( rbind(dmin,dmax)[s,] )
}
# test data
xy=NULL
xy$x=1:100000/10000
xy$y=rnorm(length(xy$x))
# first, initialize plot area so that compress can work on its coordinates
plot(NULL, xlim=range(xy$x), ylim=range(xy$y))
points(compress(xy), ty="l", col="black") # fast
points(xy, ty="l", col="red") # slow, ~100% overlap
points(compress(xy), ty="l", col="blue") # fast, ~100% overlap
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.