创建具有3个彩色段的交互式线?

时间:2016-08-10 14:23:29

标签: r ggplot2 shiny ggvis

是否可以使用有光泽的+ ggplot2,ggvis或类似内容创建如下的交互式图形?

enter image description here

这个想法是,如果用户移动50%截止值或75%截止值,颜色将会调整,并且段的名称将重新调整到每个段的中间。

1 个答案:

答案 0 :(得分:2)

因为我不确定你想要什么,所以只能猜测2段。

library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(

   sidebarLayout(
      sidebarPanel(
         sliderInput("cutoff",
                     "cutoff",
                     min = 0.01,
                     max = 1,
                     step = 0.01,
                     value = 0.5) 
      ),

      mainPanel(
         plotOutput("plot")
      )
   )
))


server <- shinyServer(function(input, output) {

   output$plot <- renderPlot({
     df <- data.frame(x = seq(0.01, 1, 0.01), y = seq(0.01, 1, 0.01), 
                      group = c(rep("a", round(input$cutoff * 100)),
                                rep("b", round(((1-input$cutoff) * 100)))
                                )
                      )
     labelPos <- data.frame(pos = c(input$cutoff - input$cutoff/2, 
                                    (input$cutoff + (1 - input$cutoff)/2)
                                    ),
                            label = c("a", "b")
                            )

     ggplot()+ 
        geom_ribbon(data = df, aes(x=x, ymin = -0.2, ymax = 0.2, fill = group), show.legend = FALSE) + 
        geom_label(data = labelPos, aes(x = pos, y = 0.3, label = label)) +
        coord_cartesian(ylim = c(-1,1)) + 
        theme(line = element_blank(),
              text = element_blank(),
              line = element_blank(),
              title = element_blank()
              )

  })
})


shinyApp(ui = ui, server = server)