Luis Rodriguez
Luis Rodriguez

Reputation: 13

Scatterplot made in ggplot2 incorrectly displaying when in shiny app

I am trying to create a basic shiny app from the iris dataset, code below. However, when I try to look at the resulting graph all of my points are collapsed, as if neither axis has a scale.

# Load libraries
library(ggplot2)
library(shiny)
library(dplyr)

# Load dataset locally
df <- iris

# Define UI for application
ui <- fluidPage(# Application title
  titlePanel("Shiny Attempt"),

  sidebarLayout(
    # sidebar
    sidebarPanel(
      # Graph 1 input
      checkboxGroupInput(
        inputId = "x",
        label = "X axis",
        choices = c("Sepal Length" = "Sepal.Length",
                    "Sepal Width" = "Sepal.Width"),
        selected = "Sepal.Width"
      ),
      checkboxGroupInput(
        inputId = "y",
        label = "Y axis",
        choices = c("Petal Length" = "Petal.Length",
                    "Petal Width" = "Petal.Width"),
        selected = "Petal.Width"
      )
    ),

    # main panel
    mainPanel(
      # Graph 1 output
      plotOutput(outputId = "graph_1"))
  ))

# Define server logic required to draw plot
server <- function(input, output) {
  output$graph_1 <- renderPlot({
    # plot inputs

    # draw the visualization
    ggplot(df, aes(
      x = input$x,
      y = input$y,
      color = Species
    )) +
      geom_point()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

If I run the ggplot portion separate from the shiny app, the graph displays properly.

    ggplot(iris, aes(
  x = Sepal.Width,
  y = Petal.Width,
  color = Species
)) +
  geom_point()

I guess I could add a scale to both axes, but when I look at other shiny app examples, it doesn't appear to be needed to display correctly. What step am I missing with the shiny app?

Upvotes: 1

Views: 60

Answers (1)

Duck
Duck

Reputation: 39585

Try this, you have to use aes_string() because your values are strings:

# Load libraries
library(ggplot2)
library(shiny)
library(dplyr)

# Load dataset locally
df <- iris

# Define UI for application
ui <- fluidPage(# Application title
  titlePanel("Shiny Attempt"),
  
  sidebarLayout(
    # sidebar
    sidebarPanel(
      # Graph 1 input
      checkboxGroupInput(
        inputId = "x",
        label = "X axis",
        choices = c("Sepal Length" = "Sepal.Length",
                    "Sepal Width" = "Sepal.Width"),
        selected = "Sepal.Width"
      ),
      checkboxGroupInput(
        inputId = "y",
        label = "Y axis",
        choices = c("Petal Length" = "Petal.Length",
                    "Petal Width" = "Petal.Width"),
        selected = "Petal.Width"
      )
    ),
    
    # main panel
    mainPanel(
      # Graph 1 output
      plotOutput(outputId = "graph_1"))
  ))

# Define server logic required to draw plot
server <- function(input, output) {
  output$graph_1 <- renderPlot({
    # plot inputs
    
    # draw the visualization
    ggplot(df, aes_string(
      x = input$x,
      y = input$y,
      color = 'Species'
    )) +
      geom_point()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 1

Related Questions