【问题标题】:Shiny, R: Check box, geom_hline, ggplot闪亮,R:复选框,geom_hline,ggplot
【发布时间】:2019-03-09 16:03:51
【问题描述】:

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

在用户界面中:

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 循环。 非常感谢您的任何建议!

【问题讨论】:

    标签: r checkbox ggplot2 shiny geom-hline


    【解决方案1】:

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

    p = ggplot(data = ......) +
          geom_bar(....) +
          theme(....)
    
    if(input$hline){
        p = p + geom_hline(....)
    }
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2015-12-02
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多