Selecting a specific Scatterplot point in ShinyPlot

Hi there,

I am trying to create a scatterplot in shiny that allows me to select a test data point through the selectInput command. I am trying to have that selection lay on top of the training data scatterplot.

# Shiny App
ui <- fluidPage(
  titlePanel("Scatter Plot of Percent Change Data"),
  sidebarLayout(
    sidebarPanel(selectInput("Name", "Please Select a Player:",choices=as.character(final_test_data$Name))),
    mainPanel(plotOutput("scatterPlot"))
  )
)


server <- function(input, output, session) {
  
  final_test_data <- read.csv("final_test_data.csv")
  final_train_data <- read.csv("final_train_data.csv")
  final_nominee_data <- read.csv("final_nominee_data.csv")

  library(ggplot2)
  
  Name_List <- final_test_data$Name
  
  Name <- reactive({input$Name_list})
  
  knn_plot <- ggplot() +
    geom_hline(yintercept = 0) + geom_vline(xintercept = 0) +
    geom_point(final_nominee_data, mapping = aes(final_nominee_data$Usage_Percent_Change,final_nominee_data$PER_Percent_Change), color = "red") +
    geom_point(final_train_data, mapping = aes(final_train_data$Usage_Percent_Change,final_train_data$PER_Percent_Change), color = "blue") +
    ylab("Percent Change in Player Efficiency Rating") + xlab("Percent Change in Usage Rating") +
    ggtitle("Most Improved Player Scatterplot")
  
  output$scatterPlot <- renderPlot({
    knn_plot + 
      geom_point(final_test_data[input$Name,], mapping = aes(final_test_data[input$Name,]$Usage_Percent_Change,final_test_data[input$Name,]$PER_Percent_Change), color = "orange")

  })
}


shinyApp(ui=ui,server=server)

With this picture, you can see that the selected data point is not plotted. Also, I get the warning message:
Warning: Removed 1 rows containing missing values (geom_point).

I'm trying to have the user select the test data to see where it lays relative to the training data. The test data color should be orange. The blue dots represent "No" and the red dots represent "Yes" in the training data. Any help would be much appreciated.

# Shiny App
library(shiny)
library(tidyverse)

data <- tibble(Name=letters[1:24],
               Usage_Percent_Change=runif(24),
               PER_Percent_Change=runif(24),
               subset = rep(c("test","train","nominee"),8)
)

final_test_data <- filter(data,
                          subset=="test")
final_train_data <- filter(data,
                           subset=="train")
final_nominee_data <- filter(data,
                             subset=="nominee")


ui <- fluidPage(
  titlePanel("Scatter Plot of Percent Change Data"),
  sidebarLayout(
    sidebarPanel(selectInput("Name", "Please Select a Player:", choices = as.character(final_test_data$Name))),
    mainPanel(plotOutput("scatterPlot"))
  )
)


server <- function(input, output, session) {
  
 
  library(ggplot2)
  
  Name_List <- final_test_data$Name
  
  Name <- reactive({input$Name_list})
  
  knn_plot <- ggplot(mapping = aes(Usage_Percent_Change,PER_Percent_Change)) +
    geom_hline(yintercept = 0) + geom_vline(xintercept = 0) +
    geom_point(data=final_nominee_data, color = "red") +
    geom_point(data=final_train_data,color = "blue") +
    ylab("Percent Change in Player Efficiency Rating") + xlab("Percent Change in Usage Rating") +
    ggtitle("Most Improved Player Scatterplot")
  
  output$scatterPlot <- renderPlot({
    knn_plot + 
      geom_point(data=final_test_data %>% 
                   filter(Name==req(input$Name)),
                 color = "orange",size=4)
    
  })
}

shinyApp(ui=ui,server=server)

Wow, it worked! Thank you so much!

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