2016-04-28 68 views
0

我想调整y轴标题为ggplot。我选择了selectizeInput小部件,以便用户可以选择多列进行绘图。我希望y轴的标题改变动态并显示选择列名。闪亮的ggplot:动态y轴标题(多列)

有了这个简单的代码:

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

它是只显示第一选择......

如果我更改为:

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

它显示只有两个选择,但如果只有一个选择,然后在轴标题上,我们将看到不适用代替第二选择。

我希望它是动态的,所以用户可以选择1个或更多的选择,yaxis的标题将要调整..

感谢您的帮助!

回答

1

如果你需要的是dunamicaly改变ylab

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

+0

谢谢!这就是我需要什么 –