将散点图和折线图与R中的高位图表结合起来

时间:2018-08-24 05:16:03

标签: r highcharts r-highcharter

我是 highcharter 软件包的新手。我想在散点图上添加一条线(回归线)。看起来很简单,但我无法破解。我可以使用hchart()分别制作它们,但它们不会合并。

library(broom)
library(dplyr)
library(highcharter)

lm.model <- augment(lm(mpg ~ wt, data = mtcars))

lm.model %>%
  hchart(type = "scatter", hcaes(x = wt, y = mpg))

lm.model %>%
  hchart(type = "line", hcaes(x = wt, y = .fitted)

我也尝试使用hc_add_series_scatter(),但找不到与行类似的功能。

highchart() %>%
  hc_add_series_scatter(x = lm.model$wt, y = lm.model$mpg)

甚至可以合并两种图表类型吗?

1 个答案:

答案 0 :(得分:3)

尝试

highchart() %>%
  hc_add_series(lm.model, "scatter", hcaes(wt, mpg)) %>% 
  hc_add_series(lm.model, "line", hcaes(x = wt, y = .fitted))

输出是

enter image description here