我想绘制图中使用的数据和操作变量(此处:rangeMin
和rangeMax
)。变量变化的后果应该在图中直接可见。
我想要有 GUI 元素(例如滑块),我可以使用它来更改变量值rangeMin
和rangeMax
调用rangePlot()
函数。
R 中是否有提供 GUI 元素的输出设备?
# generate example data
n <- 100
x <- 1:n
y <- x + (runif(n) * 25)
# rangeMin: value I'd like to manipulate using sliders
# rangeMax: value I'd like to manipulate using sliders
rangePlot <- function(x, y, rangeMin, rangeMax) {
plot(x, y)
# linear regression using datapoints a a certain range
plotrange <- c(rangeMin:rangeMax)
# linear model (y is mapped on x)
linReg = lm(formula = y[plotrange] ~ x[plotrange])
abline(linReg, col=2)
# highlight points used for liniar regression
points(x[plotrange], y[plotrange], col=2, pch=3)
# show slope and intercept in the plot
text(
x=min(x),
y=max(y),
paste0(
"intercept: ", linReg$coefficients[1],
"\nslope: ", linReg$coefficients[2]
),
adj=c(0, 1)
)
}
# manual call
rangePlot(x=x, y=y, rangeMin=1, rangeMax=n)
rangePlot(x=x, y=y, rangeMin=0.2*n, rangeMax=0.8*n)
rangePlot(x=x, y=y, rangeMin=50, rangeMax=60)
#