R Shiny互动图表的ggplot标题

4

我希望创建一个Shiny应用程序,能够显示交互式图表标题(根据所选的x轴数值而定)。

以下是一个非常简单的示例:

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()  
    })
  }
)  

我尝试了这段代码:
ggtitle("Line plot of x vs",input$yaxis)

它不起作用,图表没有显示,给了我一个错误:

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

[重要]

使用ggtitle(input$yaxis)可以为我提供一个交互式标题,但我需要构建一个句子(例如:x与input$yaxis的折线图),其中动态参数(input$yaxis)是其一部分!

感谢您的任何帮助!

干杯


4
使用ggtitle(paste("Line plot of x vs", input$yaxis))来设置图表标题,其中"Line plot of x vs"是固定的部分,而input$yaxis是根据用户输入的y轴标签动态生成的。 - Axeman
为什么会有人点踩?也许问题看起来很简单,但 S.O. 的重点是为了帮助所有人,包括新手。OP 做了好工作,在解释问题和格式化方面都很出色。显然在提问之前,他/她已经付出了一些努力。 - Mike Williamson
1个回答

9

变更:

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

To

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

正如错误提示所示,您向ggtitle函数传递了过多的参数,paste将使用一个空格将两个输入组合成一个单一字符。 您可以使用sep =来变化两者之间的分隔符。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接