rubengura
rubengura

Reputation: 469

Cannot plot properly in RShiny

I'm trying to create an easy shiny dashboard. I'm using the next data frame:

df <- data.frame(Age = c(18,20,25,30,40), 
             Salary = c(18000, 20000, 25000, 30000, 40000),
             Date = as.Date(c("2006-01-01", "2008-01-01", "2013-01-01", "2018-01-01", "2028-01-01")))

save(df, file = "data.Rdata")

And the code for doing the shiny app is the following:

library(shiny)
library(ggplot2)

load("C:/.../data.RData")


ui <- fluidPage(


sidebarLayout(

# Inputs
sidebarPanel(

  # Select variable for y-axis
  selectInput(inputId = "y", 
              label = "Y-axis:",
              choices = names(df), 
              selected = "Salary"),
  # Select variable for x-axis
  selectInput(inputId = "x", 
              label = "X-axis:",
              choices = names(df), 
              selected = "Date")
),

# Outputs
mainPanel(
  plotOutput(outputId = "scatterplot")
)
)
)


server <- function(input, output) {


  output$scatterplot <- renderPlot({
ggplot(data = df, aes(x = input$x, y = input$y)) +
  geom_line()
})
}

shinyApp(ui = ui, server = server)

This is what I get on my plot:

enter image description here

And this is what I'm expecting:

enter image description here

I'm not sure what I'm missing on my code.

Upvotes: 0

Views: 39

Answers (2)

MLavoie
MLavoie

Reputation: 9836

or simply by using:

output$scatterplot <- renderPlot({
    ggplot(data = df, aes_string(x = input$x, y = input$y)) +
        geom_line()
})

Upvotes: 1

DJack
DJack

Reputation: 4940

Try with:

  output$scatterplot <- renderPlot({
    ggplot(data = df, aes(x = df[, input$x], y = df[, input$y])) +
      geom_line()
  })

Upvotes: 1

Related Questions