Mal_a
Mal_a

Reputation: 3760

Shiny ggplot: dynamic y axis title (multiple columns)

I am trying to adjust y axis title to the ggplot. I have choosen selectizeInput widget so the user can choose multiple columns for plotting. I would like the title of y axis to change dynamicaly and display choosen columnn names.

With this simple code:

library(shiny)
library(DT)
library(ggplot2)
library(reshape2)

x <- as.numeric(1:1000000)
y <- as.numeric(1:1000000)
z <- as.numeric(1:1000000)
data <- data.frame(x,y, z)

shinyApp(
  ui = fluidPage(selectizeInput(inputId = "yaxis", 
                             label = "Y-axis",
                             choices = list("x","y","z"), 
                             selected = c("x"), multiple=TRUE),
                 dataTableOutput('tableId'),
                 plotOutput('plot1')),
  server = function(input, output) {    
    output$tableId = renderDataTable({
      datatable(data, options = list(pageLength = 10, lengthMenu=c(10,20,30)))
    })
    output$plot1 = renderPlot({

      filtered_data <- data[input$tableId_rows_all,]

      widedata <- subset(filtered_data, select=c("x", input$yaxis))

      longdata <- melt(widedata, id.vars="x", variable.name="Variables", value.name="Value_y")

      ggplot(data=longdata, aes_string(x="x",y="Value_y",group="Variables")) + geom_line() +ylab(paste(input$yaxis))   
    })
  }
)  

It is displaying only the 1st choice...

If i change to:

+ylab(paste(input$yaxis[1],input$yaxis[2]))

It displays only two choices, but if only one choice is selected, then on the axis title we will see NA in place of the second choice..

I would like it to be dynamic, so user can choose 1 or more choices, where the title of yaxis is going to adjust..

Thanks for help!

Upvotes: 0

Views: 1424

Answers (1)

Batanichek
Batanichek

Reputation: 7871

If all you need it is dunamicaly change ylab

Try +ylab(paste(input$yaxis,collapse = " ") )

Upvotes: 2

Related Questions