图形下方阴影多边形中的间隙仅在闪亮 ap 中正态分布的平均值
gap in shaded polygons below graph only at the mean of a normal distribution in shiny aps
我正在编写一个闪亮的应用程序,它根据滑块的输入创建多边形。代码在这里
SATmean=1000
SATsd=200
Score <- round ((seq(-3,3,length=120)*SATsd + SATmean), -1)
y <- dnorm(Score,SATmean,SATsd)
percentile <- round(pnorm((Score-mean)/sd)*100,2)
ui <- fluidPage (
plotOutput (outputId = "graph"),
sliderInput (inputId = "SATscore",
label = "Select your SAT score", step = 10,
value = 1200, min = 400, max = 1600)
)
server <- function(input,output){
output$graph <- renderPlot({
plot(Score, y, type="l")
polygon(c(Score[Score>=input$SATscore],rev(Score[Score>=input$SATscore])),
c(rep(0,length(Score[Score>=input$SATscore])),rev(y[Score>=input$SATscore])),col="skyblue")
polygon(c(Score[Score<=input$SATscore],rev(Score[Score<=input$SATscore])),
c(rep(0,length(Score[Score<=input$SATscore])),rev(y[Score<=input$SATscore])),col="magenta")
})
}
shinyApp(ui = ui, server = server)
除了一个例外,此代码工作正常,当滑块设置为平均值 1000 时,多边形对之间存在间隙。我不知道为什么会发生这种情况,因为其他所有多边形对都工作正常。
问题出在您的 Score
向量上,如果您打印它,您可以看到在所有向量中,两个数字之间的差距是 10,除了一个 (990-1010):
Score
[1] 400 410 420 430 440 450 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630 640 650
[27] 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900 910
[53] 920 930 940 950 960 970 980 990 1010 1020 1030 1040 1050 1060 1070 1080 1090 1100 1110 1120 1130 1140 1150 1160 1170 1180
[79] 1190 1200 1210 1220 1230 1240 1250 1260 1270 1280 1290 1300 1310 1320 1330 1340 1350 1360 1370 1380 1390 1400 1410 1420 1430 1440
[105] 1450 1460 1470 1480 1490 1500 1510 1520 1530 1540 1550 1560 1570 1580 1590 1600
这导致第一个多边形结束于 990,第二个多边形开始于 1010,因此存在间隙!
要解决这个问题,您只需增加此向量的长度(150 而不是 120):
Score <- round ((seq(-3,3,length=150)*SATsd + SATmean), -1)
你应该会得到预期的结果。
我正在编写一个闪亮的应用程序,它根据滑块的输入创建多边形。代码在这里
SATmean=1000
SATsd=200
Score <- round ((seq(-3,3,length=120)*SATsd + SATmean), -1)
y <- dnorm(Score,SATmean,SATsd)
percentile <- round(pnorm((Score-mean)/sd)*100,2)
ui <- fluidPage (
plotOutput (outputId = "graph"),
sliderInput (inputId = "SATscore",
label = "Select your SAT score", step = 10,
value = 1200, min = 400, max = 1600)
)
server <- function(input,output){
output$graph <- renderPlot({
plot(Score, y, type="l")
polygon(c(Score[Score>=input$SATscore],rev(Score[Score>=input$SATscore])),
c(rep(0,length(Score[Score>=input$SATscore])),rev(y[Score>=input$SATscore])),col="skyblue")
polygon(c(Score[Score<=input$SATscore],rev(Score[Score<=input$SATscore])),
c(rep(0,length(Score[Score<=input$SATscore])),rev(y[Score<=input$SATscore])),col="magenta")
})
}
shinyApp(ui = ui, server = server)
除了一个例外,此代码工作正常,当滑块设置为平均值 1000 时,多边形对之间存在间隙。我不知道为什么会发生这种情况,因为其他所有多边形对都工作正常。
问题出在您的 Score
向量上,如果您打印它,您可以看到在所有向量中,两个数字之间的差距是 10,除了一个 (990-1010):
Score
[1] 400 410 420 430 440 450 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630 640 650
[27] 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900 910
[53] 920 930 940 950 960 970 980 990 1010 1020 1030 1040 1050 1060 1070 1080 1090 1100 1110 1120 1130 1140 1150 1160 1170 1180
[79] 1190 1200 1210 1220 1230 1240 1250 1260 1270 1280 1290 1300 1310 1320 1330 1340 1350 1360 1370 1380 1390 1400 1410 1420 1430 1440
[105] 1450 1460 1470 1480 1490 1500 1510 1520 1530 1540 1550 1560 1570 1580 1590 1600
这导致第一个多边形结束于 990,第二个多边形开始于 1010,因此存在间隙!
要解决这个问题,您只需增加此向量的长度(150 而不是 120):
Score <- round ((seq(-3,3,length=150)*SATsd + SATmean), -1)
你应该会得到预期的结果。