rAmCharts 散点图,具有围绕拟合值的上下置信区间

rAmCharts Scattered plot with Upper and lower confidence interval around Fitted values

我正在尝试使用 rAmCharts 创建一个带有趋势线的散点图,该趋势线还将包括拟合值的上限和下限置信区间。

下面是我的数据以及基本的置信区间:

Data = data.frame(ax = rnorm(50), ay = rnorm(50))
                                            Data = cbind(Data, predict(lm(Data$ay ~ Data$ax - 1), newdata = data.frame(x = Data$ax), interval = 'prediction'))
                                            Data = Data[order(Data$ax), , drop = FALSE]

此外,上述数据的 rAmCharts 实现:

amChart_Plot = amXYChart() %>%
                  setDataProvider(dataProvider = Data, keepNA = TRUE) %>%
                  addGraph(xField = "ax", yField = 'ay', bullet = 'round', lineAlpha = 0, bulletAlpha = 0.8, bulletSize = 15) %>%   
                  addGraph(xField = "ax", yField = 'fit', bullet = 'round', lineAlpha = 0.5, bulletAlpha = 0, linetSize = 45, lineColor = '#d202fc') %>%                                                             
                  setBalloon(cornerRadius = 12, textAlign = "left", maxWidth = 1300)

amChart_Plot@valueAxes = list(list(title = paste('X-Axis', sep = ""), 
                            labelFunction = JS("function(value) {return value+'%';}"), position = 'bottom', titleBold = FALSE),
                            list(title = paste('Y-Axis', sep = ""), 
                            labelFunction = JS("function(value) {return value+'%';}"), position = 'left', titleBold = FALSE))   

amChart_Plot

如您所见,虽然我能够使用拟合值创建趋势线,但是我不能包括上限和下限置信区间,该区间内的区域可以被填充.

我在网上搜索过,但是我遇到的所有例子都只是添加趋势线。

任何关于如何在拟合值的上下置信区间内填充区域的指示都将受到高度赞赏。

这是一个可能的解决方案。

library(rAmCharts)
library(dplyr)
library(htmlwidgets)
set.seed(11223344)
Data = data.frame(ax = rnorm(50), ay = rnorm(50))
Data = cbind(Data, predict(lm(Data$ay ~ Data$ax - 1), 
                   newdata = data.frame(x = Data$ax), interval = 'prediction'))
Data = Data[order(Data$ax), , drop = FALSE]

# Create a data set with the coordinates of the border around the area
Data2 <- data.frame(
           x = c(Data$ax[1],Data$ax,rev(Data$ax)), 
           y = c(Data$lwr[1],Data$upr,rev(Data$lwr))
         )
# Put variables of "Data" into "Data2" (and fill with NAs)
Data2$ax <- c(Data$ax, rep(NA,nrow(Data2)-nrow(Data)))
Data2$ay <- c(Data$ay, rep(NA,nrow(Data2)-nrow(Data)))
Data2$fit <- c(Data$fit, rep(NA,nrow(Data2)-nrow(Data)))

amChart_Plot <- amXYChart() %>%
    setDataProvider(dataProvider = Data2, keepNA = TRUE) %>%
    addGraph(xField = "ax", yField = "ay", bullet = 'round', lineAlpha = 0, 
             bulletAlpha = 0.8, bulletSize = 15) %>%   
    addGraph(xField = "ax", yField = "fit", bullet = 'round', lineAlpha = 0.5, 
             bulletAlpha = 0,  lineThickness = 4, lineColor = '#d202fc') %>%    
    addGraph(xField = "x", yField = "y", bullet = 'round', bulletColor="transparent", 
             lineAlpha = 0.1, fillAlphas = 0.1, linetSize = 45, lineColor = '#0000FF') %>%
    setBalloon(cornerRadius = 12, textAlign = "left", maxWidth = 1300)

amChart_Plot@valueAxes = list(
    list(title = paste('X-Axis', sep = ""), 
         labelFunction = JS("function(value) {return value+'%';}"), 
         position = 'bottom', titleBold = FALSE),
    list(title = paste('Y-Axis', sep = ""), 
          labelFunction = JS("function(value) {return value+'%';}"), 
          position = 'left', titleBold = FALSE)
    )     
amChart_Plot