timelyportfolio / parcoords

R htmlwidget for parallel-coordinates chart
https://timelyportfolio.github.io/parcoords/
Other
77 stars 20 forks source link

Smoothness for lines in parallel plot #12

Open sherrylau opened 9 years ago

sherrylau commented 9 years ago

Hi,

My current plot is like this:

  parcoords(clusterFeatureData,
            rownames=F,
            reorderable=T,
            margin=list(top=20, bottom=20, left=0, right=0),
            alpha=0.9,
            color="#CC3333",
            brushMode="1D-axes-multi",
            width="1080",
            height="400")

Is that possible to change the smoothness like the one here? https://syntagmatic.github.io/parallel-coordinates/

timelyportfolio commented 8 years ago

@sherrylau, sorry it took me so long to get to this. If you are still there, here are some ways to achieve this. If popular enough, I'll add to parcoords itself.

library(parcoords)

pc <- parcoords(mtcars)

# manually add smoothness
pc$x$options$smoothness <- 0.3
library(htmltools)

pc$dependencies <- list(
  htmlDependency(
    name = "sylvester",
    version = "0.1.3",
    src = c(href="//cdnjs.cloudflare.com/ajax/libs/sylvester/0.1.3/"),
    script = "sylvester.min.js"
  )
)
pc

# make a function to add smoothness
add_smoothness <- function(pc, smoothness = 0){
  stopifnot(
    inherits(pc,"parcoords"),
    (smoothness >= 0 && smoothness <= 1)
  )

  pc$x$options$smoothness <- smoothness

  pc$dependencies[[length(pc$dependencies) + 1]] <- htmlDependency(
      name = "sylvester",
      version = "0.1.3",
      src = c(href="//cdnjs.cloudflare.com/ajax/libs/sylvester/0.1.3/"),
      script = "sylvester.min.js"
    )
  pc
}

pc2 <- parcoords(mtcars)
add_smoothness(pc2, 0.1)
ZJUguquan commented 7 years ago

@timelyportfolio , Hi,

I have followed your advice and the lines become smoother as planed. However, it seems that the smoother parcoords photo can not be render by shiny using renderParcoords and parcoordsOutput. If I do like this, all polylines disappear... >_<

The R codes is below,

library(shiny)
library(parcoords)
library(htmltools)
add_smoothness <- function(pc, smoothness = 0){
  stopifnot(
    inherits(pc,"parcoords"),
    (smoothness >= 0 && smoothness <= 1)
  )
  pc$x$options$smoothness <- smoothness
  pc$dependencies[[length(pc$dependencies) + 1]] <- htmlDependency(
    name = "sylvester",
    version = "0.1.3",
    src = c(href="//cdnjs.cloudflare.com/ajax/libs/sylvester/0.1.3/"),
    script = "sylvester.min.js"
  )
  pc
}

shinyApp(
  ui = fluidPage(parcoordsOutput('pcoords')),
  server = function(input, output){
    output$pcoords <- renderParcoords({
      pc <- parcoords(mtcars)
      pc2 <- add_smoothness(pc, 0.2)
      return(pc2)
    })
  }
)

so what should I do?

Waiting for your reply~

timelyportfolio commented 7 years ago

It appears the dependency attached to the htmlwidget is not working. I am a little surprised by this, but fortunately we can take a different approach. Try this way.

library(shiny)
library(parcoords)
library(htmltools)
add_smoothness <- function(pc, smoothness = 0){
  stopifnot(
    inherits(pc,"parcoords"),
    (smoothness >= 0 && smoothness <= 1)
  )
  pc$x$options$smoothness <- smoothness
  pc
}

shinyApp(
  ui = tagList(
    htmlDependency(
      name = "sylvester",
      version = "0.1.3",
      src = c(href="//cdnjs.cloudflare.com/ajax/libs/sylvester/0.1.3/"),
      script = "sylvester.min.js"
    ),
    fluidPage(parcoordsOutput('pcoords'))
  ),
  server = function(input, output){
    output$pcoords <- renderParcoords({
      pc <- parcoords(mtcars)
      pc2 <- add_smoothness(pc, 0.2)
      return(pc2)
    })
  }
)
ZJUguquan commented 7 years ago

@timelyportfolio Thank you very much! It works!