【问题标题】:How to add a regression line to Shiny rCharts plot如何在 Shiny rCharts 图中添加回归线
【发布时间】:2014-07-21 19:22:12
【问题描述】:

我正在尝试在 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)
  })

【问题讨论】:

  • 如果你得到你想要的,请接受答案。

标签: r shiny rcharts


【解决方案1】:

请检查下面我从@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2021-01-14
    • 2020-03-13
    • 2016-02-24
    相关资源
    最近更新 更多