-3

I am making a dashboard that plots an x and y input. However when both inputs are passed, ggplot plots my data into a singular line. I have also tried using plotly and I get no result altogether. Would someone be able to help with a working ggplot and plotly example.

here is the ggplot code portion plus image result

renderPlot({
   p <- ggplot(Merged_data_frame_hcat, aes_string(x=input$x, y=input$y)) + geom_point()
 
   print(p)
  
})

enter image description here

and the plotly code plus image

renderPlot({
  p <- plot_ly(data= Merged_data_frame_hcat,x= ~input$x, y= ~input$y,type = 'scatter', mode = 'lines' )
 
   print(p)
  
})

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78
Bcarine
  • 35
  • 4
  • 5
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. What exactly is in your data.frame? Are you sure you are passing correct column names to `aes_string()` – MrFlick Aug 17 '20 at 21:12
  • use `cat(input$x,'\n')` before plotting to debug your code : looks like it's equal to 1 – Waldi Aug 17 '20 at 21:27
  • Looks like whatever `input$x` is... it's possibly numeric. Otherwise... well, it's equal to 1. – chemdork123 Aug 17 '20 at 21:27
  • df <-data.frame("Timestamp.Excel_1900."=c("2019-04-01 16:52:51","2019-04-01 16:57:46","2019-04-01 17:02:51","2019-04-01 17:07:46","2019-04-01 17:12:52","2019-04-01 17:17:46"), "Temperature.C."= c(5.2995,5.3155,5.3353,5.3536,5.3770,5.4044)) – Bcarine Aug 17 '20 at 23:49
  • Here are some of the values that I am trying to plot as a data frame. @Waldi I tried p <- ggplot(Merged_data_frame_hcat, aes_string(x=cat(input$x,'\n') y=input$y)) + geom_point() and I got Error:geom_point requires the following missing aesthetics: x and y – Bcarine Aug 17 '20 at 23:54
  • @MrFlick here are some the inputs I am passing radioButtons( "x", h3("X Variable"), choices = list("date" = 1), selected = "class", choiceValues = c("Timestamp.Excel_1900.") ) checkboxGroupInput( 'y', h3("Y Variables"), choiceNames = c("temperature [C]") ) – Bcarine Aug 17 '20 at 23:59

1 Answers1

1

to debug your code, you can print the data you are using in the console using cat, for example:

renderPlot({
   cat('input$x=',input$x,'\n')
   p <- ggplot(Merged_data_frame_hcat, aes_string(x=input$x, y=input$y)) + geom_point()
   print(p)
  
})

If you look at the RStudio console, you'll see the value you're passing to aes_string.
From your answers in the comments, you'll most probably see in the console:

shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1
input$x = 1

This is due to the way selectInput works :

choices : List of values to select from. If elements of the list are named, then that name --- rather than the value --- is displayed to the user.

This means that if

choices = list('dates'=1)

You see dates in the selectInputand you get 1 in input$x.
For further help, as pointed out by @MrFlick in the first comment, you'll need to provide a simple reproducible example : discovering & solving a problem mainly through comments isn't efficient.

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • I am new to using shiny so I was using a example code for the select input. Would it work if i just remove the 1 . and would the corrections you pointed above still work if there was other x inputs – Bcarine Aug 18 '20 at 03:07
  • I got it to work by removing the 1 and leaving it as radioButtons( "x", h3("X Variable"), choices = c("date"="Timestamp.Excel_1900.")) ggplot displayed the data point a bit grouped up. I did some research on plotly and it seems like it helps you zoom in to specific dates. However the same code with plotly is still not working. – Bcarine Aug 18 '20 at 03:26