简体   繁体   中英

R Shiny Interactive plot title for ggplot

I am trying to create Shiny App which is able to display interactive plot title (dependent on the choosen value for x axis)

Very simple example:

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

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

shinyApp(
  ui = fluidPage(selectInput(inputId = "yaxis", 
                             label = "Y-axis",
                             choices = list("x","y","z"), 
                             selected = c("x")),
  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, ]
      ggplot(data=filtered_data, aes_string(x="x",y=input$yaxis)) + geom_line()  
    })
  }
)  

I have tried this code:

ggtitle("Line plot of x vs",input$yaxis)

It was not working, plot has not been displayed, giving me an Error:

Warning: Error in ggtitle: unused argument (input$yaxis)

[IMPORTANT]

using ggtitle(input$yaxis) gives me an interactive title, however i need to build up a sentence (like: Line plot of x vs input$yaxis ), in which the reactive argument ( input$yaxis ) is a part of it!

Thanks for any help!

Cheers

Change:

ggtitle("Line plot of x vs",input$yaxis)

To

ggtitle(paste("Line plot of x vs",input$yaxis))

As the error suggests, you have too many arguments passed to the ggtitle function, paste will create a single character out of your two inputs, with a space in between. You can vary the separation between the two with sep = .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM