我想知道在RStudio中是否可以使用滑块绘制交互式图形。我想绘制一条直线,我想创建一个改变截距和斜率的滑块。 例如this,其中a和b是我的滑块,我可以根据需要自由移动。
答案 0 :(得分:1)
这将有效:
library(manipulate)
x <- seq(from = 0, to = 100, by = 0.1)
manipulate(plot(x,slope*x+intercept), slope = slider(0, 100),
intercept = slider(-100,100))
另一个选择是创建一个闪亮的应用程序:
#save this script as app.R
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Linear Equation App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "slope",label = "Slope:",
min = 0,max = 100,value = 0),
sliderInput(inputId = "intercept",label = "Intercept",
min = -100,max = 100,value = 0)
),
mainPanel(
plotOutput("lineplot")
)
)
)
server <- function(input, output) {
output$lineplot <- renderPlot({
x <- seq(from = 0, to = 100, by = 0.1)
y <- x*input$slope + input$intercept
plot(x,y)
})
}
shinyApp(ui = ui, server = server)
答案 1 :(得分:1)
这是一个有光泽+ ggplot2的简单示例。足够容易创建一条线,其中x1 = 1,x2 = 2,并且y是基于来自滑块的输入使用y1 = mx1 + b计算的; y2 = mx2 + b。
library(ggplot2)
library(shiny)
ui <- fluidPage(
strong("This is an interactive line"),
sliderInput("slope", "Define slope:", min = -100, max = 100, value = 0, step = 0.01),
sliderInput("intercept", "Define intercept:", min = -10000, max = 10000, value = 0, step = 1),
plotOutput("linePlot"))
server <- function(input, output) {
output$linePlot <- renderPlot({
ggplot(mapping = aes(x = c(1, 2),
y = c(input$slope*1+input$intercept, input$slope*2+input$intercept))) +
geom_line()
})
}
shinyApp(ui, server)