0

I would like to produce a 3d scatter plot which is rotatable with the mouse. I am happy to have this with no pop-up window (though this is not a must-have, but makes things easier for now.). I can do this happily with renderRglwidget(). I managed as well to add a slider which changes the range of the x-variable.

Now things get over my head: I would like to switch between different variables for the x-axis, but keep all other features. (I managed a switch by itself, but then other features go missing!)

Once this is mastered, I will want to have all three axis in this flexible manner: choose the variable and change the range shown. Meaning that each numeric variable could be on any axis in any combination (aside from choosing the same variable twice, of course).

Thinking way ahead (!!) it would be cool to have the possibility to show different colors depending on a dummy variable. Phew! (Linear) regression lines don't make much sense to me as the real life data is badly behaved with below-earth goodness-of-fit-values (10% or below!).

I generate some play data below as I cannot publish my data. My data is nowhere close to normally distributed and has outliers.

set.seed(1)
somedata <- data.frame(
    v1 = rnorm(2000, 0, 1),
    v2 = sample(c("yes", "no"), 2000, replace = TRUE),
    v3 = rnorm(2000, 100, 15),
    v4 = sample(c("blue", "green", "red"), 2000, replace = TRUE),
    v5 = rnorm(2000, 10, 15),
    v6 = rnorm(2000, 10, 15)
)
somedata$v1 <- ifelse(somedata$v2 == "yes", somedata$v1 + 1000, somedata$v1)

Here is my adjusted code

library(rgl) #plot widget
library(car) # scatter3d
library(shiny)
library(dplyr)# "select", "filter"


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(

      helpText("Just some text"),

      selectInput("varx",
                  label = "x-Variable", 
                  choices = c("textx1", "textx2"),
                  selected = "textx1"),

      sliderInput("rangex",
                  label = "Range of X", 
                  min = -5000, max = 9000, value = c(-5000, 9000)),

      textOutput("SliderTextx")
  ), #sidebarPanel
  mainPanel(

      rglwidgetOutput("plot",  width = 800, height = 600)
  )#mainPanel
  )#sidelayout
)#fluidpage

server <- function(input, output) {
  chosendata <-  reactive({
    # make sure inputs are not Null
    req(input$rangex)

    #filter data according to inputs
    chosendata <- filter(somedata, between(v1, input$rangex[1], input$rangex[2]))
    return(chosendata)
  })#reactive

  # ensures that our filter works
  observe(print(str(chosendata())))

  #create a graph
  output$plot <- renderRglwidget({
    req(chosendata())
    active <- chosendata()
    rgl.open(useNULL=T)
    scatter3d(x= active$v1, y= active$v5, z= active$v6, 
              surface=FALSE, ellipsoid = TRUE)
    rglwidget()
  })

}#function   

shinyApp(ui = ui, server = server)

This example should run. I would like to have the choice of all three axis variables and their specific range. However, between(input$varx,... and scattered(x=active$varx,...I cannot get to work. I am guessing that once I have it sorted for x it should be the same for y and z.

On top of it I would like to see the grouping.

I scooped around the internet where I can find a solution to either-or challenge, but I cannot manage to put it together. I hope I wrote this in an understandable manner.

I am looking forward to your replies!

Gerit

Gerit
  • 195
  • 2
  • 14

1 Answers1

0

Unfortunately, your example code produced an error for me.

Here's a toy example that I hope demonstrates the functionality you want: you can select any variables on the x-axis, y-axis and (as a factor) as a grouping variable. Obviously, not every combination is sensible!

library(shiny)
library(tidyverse)

ui <- fluidPage(
  wellPanel(
    selectInput("x", "X axis variable", choices=c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"), selected="wt"),
    selectInput("y", "Y axis variable", choices=c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")),
    selectInput("group", "Grouping variable", choices=c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"), selected="cyl")
  ),
  wellPanel(
    plotOutput("plot")
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    req(input$group, input$x, input$y)

    mtcars %>% 
      ggplot(aes_string(x=input$x, y=input$y)) +
      geom_point(aes(colour=as.factor(!!as.name(input$group))))
  })
}

shinyApp(ui, server)
Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thank you, Limey, for your reply. It already shows another point I would like to make. And you encouraged me to try again to show what I would like to see. I hope this runs through. – Gerit Jun 16 '20 at 09:18