Using brushedPoints with custom coord transformation.

I am having trouble selecting points using a brush when I use a custom axis-transformation. I have pasted a snippet of code below. Basically the app works as expected, when I don't use transformation but not with asinh transformation.

library(ggplot2)

shinyApp(
  ui = fluidPage(
    selectInput(inputId = 'axis_trans',
                label = 'Transform Axis: ',
                choices = list("None", "log2", "asinh")
    ),
    plotOutput("bPlot", brush=brushOpts("bBrush")),
    fluidRow( column(5, "Brushed: ", verbatimTextOutput( "brushed" )))
    
  ),
  
  server = function(input, output, session) {
    asinhco_trans <- function(cofactor) {
      require(scales)
      trans_new("asinhco", 
                transform=function(x) {
                  if (cofactor !=0) {
                    xt <- asinh(x/cofactor)
                  } else {
                    xt <- asinh(x)
                  }
                  return(xt)
                },
                inverse= function(xt) {
                  if (cofactor != 0) {
                    x <- cofactor*sinh(xt)
                  } else {
                    x <- sinh(xt)
                  }
                  return(x)
                }
      )
    }
    output$bPlot <- renderPlot({
      p <- ggplot( mtcars, aes_string( x="mpg", y="wt") ) + geom_point() 
      if (input$axis_trans == "asinh") {
        p <- p + coord_trans(x=asinhco_trans(10), y=asinhco_trans(10))
      }
      if (input$axis_trans == "log2") {
        p <- p + coord_trans(x="log2", y="log2")
      }
      return(p)
    })
    output$brushed <- renderText({
      nrow(brushedPoints(mtcars,input$bBrush))
    })
  }
  
)

I was able to solve this by applying the transformation directly to the data, instead of the axis.

Would you mind sharing the code you have used to solve your problem and markin your post as a solution to your topic? This could be helpful to other people facing the same problem.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.