ramnathv / rCharts

Interactive JS Charts from R
http://rcharts.io
Other
1.19k stars 654 forks source link

Turning off sampling or raising upper limit of datapoint numbers in PolyCharts. #621

Open WMBEdmands opened 9 years ago

WMBEdmands commented 9 years ago

It has long been a source of frustration that when plotting multivariate data (i.e. >500) that PolyChart samples the data. I tried NVD3 charts to circumvent this problem but much prefer PolyCharts aesthetically speaking that is. I found this is a known issue: https://github.com/Polychart/polychart2/issues/4 I noticed at the bottom of this issue a comment from https://github.com/xuexue with a link to the PolyCharts github wiki: https://github.com/Polychart/polychart2/wiki/Layer#sample Please see an example of a shiny app including an rChart scatter plot using the Polycharts library and with the option to switch off this sampling when plotting large numbers of data-points. I hope this is useful/helpful.

library(shiny)
library(rCharts)
library(MASS)
##Global.R
###example correlated large data frame (10000 rows, 2 columns) from http://www.econometricsbysimulation.com/2014/02/easily-generate-correlated-variables.html
mu <- rep(0,2)
Sigma <- matrix(.7, nrow=2, ncol=2) + diag(2)*.3
example.df<-data.frame(round(mvrnorm(n=1000, mu=mu, Sigma=Sigma),digits=2))
colnames(example.df)<-c("X","Y")

###server.R
server<-shinyServer(function(input, output, session){ 
  ###reactive subset example.df
  Sub.df_func<-reactive({
    Sub.df<-example.df[1:input$nVariables,]
    return(Sub.df)
  })
  ###render polychart
  output$Example<-renderChart({  
    ###subset example.df
    Sub.df<-Sub.df_func()
    ###lowess smooth line
    LoessLine<-data.frame(loess.smooth(x = Sub.df$X,y = Sub.df$Y,span=input$Loess_smooth))

    ###polycharts plot
    if(input$sampling=="No")
    {
    p1<-rPlot(x = "X", y = "Y", data=Sub.df, type = "point",size = list(const = 4),sample=FALSE)
    } else {
    p1<-rPlot(x = "X", y = "Y", data=Sub.df, type = "point",size = list(const = 4))  
    }
    p1$set(width = 1000, height = 700)
    p1$set(dom="Example")
    p1$layer(y~x,data=LoessLine,type="line")
    return(p1)
  })
}) 

###ui.R
ui<-shinyUI(fluidPage(
  titlePanel("Loess_smooth_polycharts_example"),
  fluidRow(column(width=10,
                  sliderInput("Loess_smooth","Select the Loess smoother span :",min=0.01,max=1,value=0.66),
                  sliderInput("nVariables","Select the number of points to display",min=50,max=1000,value=600),
                  selectInput("sampling","Data-point sampling (>500 data-points):",choices=c("Yes","No")),
                  chartOutput("Example","PolyCharts")
                  ))))
##runApp
shinyApp(ui=ui, server=server)
ramnathv commented 9 years ago

This is very useful. Thanks.

aloboa commented 9 years ago

The short story is use sample=FALSE such as in:

x <- c(rnorm(1326/2,5,2.5),rnorm(1326/2,10,5))
y <-  rnorm(1326,15,2.5) + runif(1326,-5,5)
dftest <- data.frame(ID=1:1326, x=x, y=y,
                     cl=sample(LETTERS[1:8],1326,replace=TRUE))
rPlot(y ~ x, data = dftest, color = "cl", type = "point",
             size=list(const=2), sample=FALSE)

Note the legend is wrong as the nb of categories >7

LoopyFoot commented 9 years ago

Could this be the reason for my issue#654?

https://github.com/ramnathv/rCharts/issues/654

I'm using nplot on a large data set, and my RStidio is freezing.