R ggplot2带无功几何线

R ggplot2带无功几何线,r,ggplot2,shiny,R,Ggplot2,Shiny,我想知道如何根据无功输入添加线路。例如,如果我有一个复选框GroupInput(如下)来选择保险类型,我们如何根据所选的保险类型向ggplot添加线。我还希望能够根据checkboxGroupInput添加多行进行比较。在UI方面,我们可以有如下内容: checkboxGroupInput(inputId = "Model",

我想知道如何根据无功输入添加线路。例如,如果我有一个复选框GroupInput(如下)来选择保险类型,我们如何根据所选的保险类型向ggplot添加线。我还希望能够根据checkboxGroupInput添加多行进行比较。在UI方面,我们可以有如下内容:

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

Mainplot您可以使用
select
from
dplyr
来选择要显示的数据框列,
tidyr
包的
collect
来收集数据框中的列,并按颜色将它们分开:

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)
图书馆(tidyr)
图书馆(dplyr)

ui您是否正在尝试或控制/过滤打印的数据组?您应该给出更具重现性的示例,或使用基本data.frames,例如
mpg
。您需要创建一个对用户输入有反应的数据帧,然后让ggplot2引用该数据帧来渲染打印。您不能将像
geom_line
这样的函数设置为反应对象。您好,感谢@shosaco创建了最小可复制示例,并感谢大家的输入。这可能是可行的,我想从公式中创建一个模型,例如,a+Bx,然后绘制这个。用户可以选择要应用于模型的数据(输入选项)。我会在选定的数据上运行选定的模型,然后cbind()将模型放入data.frame,并在该数据集上运行此代码吗?再次感谢您提供的解决方案,但我认为它似乎产生了以下问题:错误:所有select()输入必须解析为整数列位置。以下内容不适用:““mpg”*input$line
packageVersion(“dplyr”)
可能已过时,请尝试使用
install.packages(“dplyr”)更新。是的,这很有效!代码在Rstudio中运行!非常感谢你
Mainplot <- renderPlot({                         
 ggplot(df_norm,aes(x=ages)) +     
 Line()
 )
                        })
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)