ggplot2与反应性Geom线 [英] ggplot2 with reactive Geom lines

查看:79
本文介绍了ggplot2与反应性Geom线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据反应性输入添加Geomlines.例如,如果我有一个checkboxGroupInput(在下面)来选择保险类型,那么我们如何根据选定的保险类型向ggplot添加行.我还希望能够基于checkboxGroupInput添加多行进行比较.在用户界面方面,我们可能如下所示:

I was wondering how to add Geomlines based on the reactive input. For example, if I have a checkboxGroupInput (below) to select insurance types, how can we add lines to the ggplot based on the selected insurance types. I would also like to be able to add multiple lines for comparison based upon the checkboxGroupInput. On the UI side we could have as below:

                      checkboxGroupInput(inputId = "Model",                                                                               
                              label = h4("What kind of insurance do you use"),                                                                       
                              choices = c("Compound" = "Compound", "Simple" = "Simple", 
                              "Private" = "Private", "Complex" = "Complex"),
                              selected = "Private"
                                                        )

然后在服务器端

Mainplot <- renderPlot({                         
 ggplot(df_norm,aes(x=ages)) +     
 # ADD GEOM_LINE HERE
 )
                        })

我尝试根据输入创建对象,然后将其添加到绘图中,但是这始终会产生错误.例如

I tried creating an object based on the input then adding it into the plot, however this creates errors throughout. e.g.,

Line <- if ((input$Model == "Private") {"geom_line(aes(y=Private_line), size=0.5) +"} else { "  "})

然后在情节中

Mainplot <- renderPlot({                         
 ggplot(df_norm,aes(x=ages)) +     
 Line()
 )
                        })

但是,这会产生错误"ggplot不知道如何处理RHS".

However, this produces the error "ggplot does not know how to handle RHS".

感谢您提供的帮助, 圆锥形

Thanks for any help you cab provide, Conal

推荐答案

您可以使用dplyr中的select来选择要显示的数据框的列,使用tidyr包中的gather进行收集在数据框中并按颜色将其分开:

You can use select from dplyr to select the columns of your dataframe that shall be displayed, gather of tidyr package to collect them in a dataframe and separate them by color:

library(ggplot2)
library(tidyr)
library(dplyr)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput(inputId = "line",                                                                               
                         label = h4("What would you like to plot?"),                                                                       
                         choices = names(mtcars))

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

server <- function(input, output) {
  output$plot <- renderPlot({             
    validate(need(!is.null(input$line), 'Please tick a box to show a plot.'))
    # 1. mpg is our fixed x-value
    # 2. select variables of mtcars you need using select()
    # 3. gather all selected variables in two columns: variable/value (but not mpg since that is the x value)
    data <- gather(select(mtcars, "mpg", input$line), variable, value, -mpg)

    # the plot, coloured by variable
    ggplot(data, aes(x=mpg, y = value, colour = variable)) + geom_point()
  })
}

shinyApp(ui, server)

这篇关于ggplot2与反应性Geom线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆