我正在尝试使用带有趋势线的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


javascript - rAmCharts散点图,上下置信区间位于拟合值附近-LMLPHP

09-11 00:15