データをプロットし、プロットで使用される変数を操作したい (ここではrangeMin
とrangeMax
)。変数の変更の結果は、プロットに直接表示される必要があります。
rangeMin
およびの変数値を変更しrangeMax
、関数を呼び出すために使用できる GUI 要素 (スライダーなど) が必要ですrangePlot()
。
GUI要素を提供するRに出力デバイスはありますか?
# 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)
#