如何在Shiny rCharts图表中添加回归线

3
我正在尝试在Shiny应用程序中使用rCharts向简单的散点图添加回归线,但无法使其正常工作。如果您删除p $ layer()调用,则以下代码将显示图形(除了回归线)。但是,使用该调用时,图形的轴被隐藏,并且没有线条,尽管点仍然显示。我做错了什么?感谢您的帮助。
output$kplot = renderChart({

     # Create dependency on submit button.
     input$submit

     # Add point when submit button is clicked.
     dataset = isolate(rbind(dataset, c(input$add_x, input$add_y)))

     # Re-fit data.
     dataset$fit = lm(dataset$mpg ~ dataset$hp)$fitted.values

     # Calculate graph minimums and maximums
     xmin = max(min(dataset$hp) - 10, 0) # Cut threshold at zero.
     xmax = (max(dataset$hp) + 11) + (50 - ((max(dataset$hp) + 10) %% 50))
     ymin = 0
     ymax = (max(dataset$mpg) + 1) + (5 - ((max(dataset$mpg) + 5) %% 5))

     p = rPlot(mpg ~ hp, data=dataset, type="point")
     p$addParams(dom="kplot")
     p$guides(
        x = list(
           min = xmin,
           max = xmax,
           numticks = xmax / 50 + 1,
           title = "HP"
        ),
        y = list(
           min = ymin,
           max = ymax,
           numticks = ymax / 5 + 1,
           title = "MPG"
        )
     )
     p$layer(y=dataset$fit, copy_layer=T, type="line", color=list(const="red"))
     return(p)
  })

请接受此回答,如果您得到了您想要的。 - BigDataScientist
1个回答

2
请查看下面的代码,它是从@ramnathv的github复制的。 https://gist.github.com/ramnathv/7576061
require(ggplot2)
require(rCharts)

dat = fortify(lm(mpg ~ wt, data = mtcars))
names(dat) = gsub('\\.', '_', names(dat))

p1 <- rPlot(mpg ~ wt, data = dat, type = 'point')
p1$layer(y = '_fitted', copy_layer = T, type = 'line',
  color = list(const = 'red'),
  tooltip = "function(item){return item._fitted}")
p1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接