情节线性趋势线没有更新闪亮

时间:2016-05-13 00:27:38

标签: r plot ggplot2 shiny plotly

我正在尝试在闪亮的应用中添加线性趋势线到情节图。当我更改选择参数时,我看到线性模型的系数发生了变化(使用observe(print(summary(l))。但是,图上的实际线条似乎保持在同一位置。

这是一个图,其中趋势线至少似乎接近两点相交: enter image description here

在另一个情节中,趋势线远不及第一点: enter image description here

这是一个最小的工作示例:

library(dplyr)
library(shiny)
library(plotly)

df <- as.data.frame(list("UserID"=c(1,1,1,1,2,2,2,2), 
                          "QuestionID"=c(4,4,5,5,4,4,6,6),
                          "KeystrokeRate"=c(8,4,6,15,8,6,7,8),
                          "cumul.ans.keystroke"=c(1,7,1,5,1,14,1,9),
                "Relative.Time.Progress"=c(0.1,1.0,0.4,1.0,0.8,1.0,0.8,1.0)
                    ))

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", sort(unique(df$UserID)),
                  selected = sort(unique(df$UserID))[1]),
      uiOutput("answerOutput")#,
    ),

    mainPanel(
      plotlyOutput("mainPlot")#,
    )
  )
))

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

  # filter only based on selected user
  filteredForUser <- reactive({
    try(
      df %>%
        filter(
          UserID == input$userInput
        ), silent=T)
  })

  # filter for both user and answer
  filteredFull <- reactive({
    try (
      df %>% 
        filter(
          UserID == input$userInput,
          QuestionID == input$answerInput
        ), silent=T)
  })

  # filter answer choices based on user
  output$answerOutput <- renderUI({
    df.u <- filteredForUser()
    if(!is.null(df)) {
      selectInput("answerInput", "Select A Typing Session",
                  sort(unique(df.u$QuestionID)))
    }
  })

  output$mainPlot <- renderPlotly({

    if (class(filteredForUser()) == "try-error" ||
        class(filteredFull()) == "try-error") {
      return(geom_blank())
    } else {
      # plot scatter points and add trend lines
        p <- plot_ly(filteredFull(), x=Relative.Time.Progress, y=cumul.ans.keystroke,
                       mode='markers', color=KeystrokeRate, size=KeystrokeRate,
                       marker=list(sizeref=100), type='scatter')
        l <- lm(cumul.ans.keystroke ~ Relative.Time.Progress,
                   data=filteredFull())
        observe(print(summary(l)))
          p <- add_trace(p, y= fitted(l))
          p
    }
  })     
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

问题在于add_trace功能。您需要提供x轴以便能够正确绘制lm结果。

p <- add_trace(p, y = fitted(l), x = Relative.Time.Progress)

要更清楚地查看问题,请使用整个数据集可视化结果。

p <- plot_ly(df, x=Relative.Time.Progress, y=cumul.ans.keystroke,
             mode='markers', color=KeystrokeRate, size=KeystrokeRate,
             marker=list(sizeref=100), type='scatter')
p

enter image description here

l <- lm(cumul.ans.keystroke ~ Relative.Time.Progress,
        data=df)
p <- add_trace(p, y = fitted(l))
p

enter image description here

p <- plot_ly(df, x=Relative.Time.Progress, y=cumul.ans.keystroke,
             mode='markers', color=KeystrokeRate, size=KeystrokeRate,
             marker=list(sizeref=100), type='scatter')
l <- lm(cumul.ans.keystroke ~ Relative.Time.Progress,
        data=df)
p <- add_trace(p, y = fitted(l), x = Relative.Time.Progress)
p

enter image description here

正如您所看到的,add_trace正在正确地绘制fitted(y),但使用x轴为c(0:7)。我猜它是传递给add_trace的默认值,但我没有深入研究过这个'为什么'。数据集df有八个点。您需要在x轴上给出实际的Relative.Time.Progress值,以正确绘制fitted(y) w.r.t.实际的x值。希望这澄清。