3

データをプロットし、プロットで使用される変数を操作したい (ここではrangeMinrangeMax)。変数の変更の結果は、プロットに直接表示される必要があります。

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)
#
4

1 に答える 1

2

コメントを詳しく説明します。RStudio の manage パッケージを使用すると、このような作業が非常に簡単になります。そのパッケージは RStudio 内で動作します。RStudio を使用せずに同じ構文を使用する場合は、gWidgets2 に例があります (gWidgetsManipulate は必要ありません)。

require(tcltk); require(tkrplot)
require(gWidgets2)                      # require(devtools); install_github(c("gWidgets2", "gWidgets2tcltk"), "jverzani")
options(guiToolkit="tcltk")
source(system.file("examples", "manipulate.R", package="gWidgets2"))

w <- gwindow("Manipulate example", visible=FALSE)

manipulate({
  y <- get(distribution)(size)
  plot(density(y, bw=bandwidth/100, kernel=kernel))
  points(y, rep(0, size))
},
           ##
           distribution=picker("Normal"="rnorm", "Exponential"="rexp"),
           kernel=picker("gaussian", "epanechnikov", "rectangular",
             "triangular", "cosine"),
           size=picker(5, 50, 100, 200, 300),
           bandwidth=slider(0.05 * 100, 2.00 * 100, step=0.05 * 100, initial=1* 100), # integers needed
           button=button("Refresh"),

           container=w
           )
visible(w) <- TRUE

GUI は RGtk2 または Qt バックエンドのどちらかで見栄えが良くなりますが、通常はインストールが少し難しくなります。

于 2013-04-05T15:42:43.517 に答える