【问题标题】:rAmCharts Scattered plot with Upper and lower confidence interval around Fitted valuesrAmCharts 散点图,具有拟合值周围的置信区间上限和下限
【发布时间】:2018-04-27 06:44:19
【问题描述】:

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

如您所见,虽然我能够使用拟合值创建趋势线,但我无法将置信区间的上限和下限包含在该区间内的区域可以填充。

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

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

【问题讨论】:

    标签: javascript r amcharts


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2019-03-02
      • 2011-08-27
      • 1970-01-01
      相关资源
      最近更新 更多