-2

在我闪亮的应用程序中,我只想在ggplot用户选择使用复选框时添加 geom_hline,我也希望用户yintercept使用numericInput. 我相信有一个简单的解决方案,但由于我不是编码员,所以我想问一下哪种方法最简单。我的代码是这样的:

在用户界面中:

numericInput('hline', label ='Limits', 0)

在服务器中:

 plotInput <- reactive({



    ggplot(data = dataforplot(), aes(x = ID_Polymer, y = value), position = position_dodge(width = 1))  +
      geom_bar(aes_string( fill=razeni()), position = position_dodge(width = 1), stat="identity", color="white")+
      theme_minimal() +
      theme(legend.text=element_text(size=21))+
      theme(text = element_text(size=21))+
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
      ggtitle(input$title_text_box_id) + 

      geom_hline(aes(yintercept = input$hline, linetype = "Minimal limit"), color='red', size=0.4)+

      labs(x = "", y = input$ylabel_text_box_id) + 
      geom_text(aes(x = ID_Polymer, y = value,Group=Polymer,label=value), 
                position = position_dodge(width = 1),vjust=2, size=5,colour = "white", fontface = "bold") +
      scale_fill_tableau("Tableau 10")+
     scale_x_discrete(labels=c(xpopisky()))#puts a reactive in x labels

  })

哪个有效,只是我不知道告诉闪亮“geom_hline仅当我通过选中复选框告诉你时才显示”的最佳方式,if else我相信应该涉及一个循环。非常感谢您的任何建议!

4

1 回答 1

0

您可以将 ggplot 对象分配给变量。下面的伪代码演示了这一点:

p = ggplot(data = ......) +
      geom_bar(....) +
      theme(....)

if(input$hline){
    p = p + geom_hline(....)
}

请注意,如果您提供了一个最小的工作示例,您的问题将会得到改善。现在,您问题中的代码包含不相关的详细信息(例如 ggplot 主题和标签),无法按原样运行。

于 2018-10-04T20:04:15.820 回答