Lecture3: Manipulate関数

平方根

arg <- 4
sqrt <- sqrt(arg)
paste("平方根は",arg)
## [1] "平方根は 4"

実習1: printSQRT関数を作成

printSQRT実行例

printSQRT(3)
## [1] "平方根は 3"

getRelativeFreqMtx.Rを読み込む

source("getRelativeFreqMtx.R")
freqMtx<-getRelativeFreqMtx("osaka-u.txt")

上位データ

head(freqMtx)
##           term raw   relative
## 211        the  33 0.06470588
## 21         and  31 0.06078431
## 146         of  31 0.06078431
## 228 university  16 0.03137255
## 112         in  15 0.02941176
## 149      osaka  15 0.02941176

データのサイズ(次元)確認

dim(freqMtx)
## [1] 246   3

補足:ソート関数(sort, order)

tmp <-freqMtx[1:10,]$raw
tmp
##  [1] 33 31 31 16 15 15 11 11 11  8
sort(tmp)
##  [1]  8 11 11 11 15 15 16 31 31 33
order(tmp)
##  [1] 10  7  8  9  5  6  4  2  3  1

単語頻度数分布

subMtx <-as.table(freqMtx[1:20,2])
names(subMtx) <-freqMtx[1:20,]$term

title="Word Frequency Distribution"
xlabel="Word"
ylabel="Frequency"
barplot(subMtx, main=title, xlab=xlabel, ylab=ylabel,las=3)

単語頻度数分布(色付き)

colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") 
barplot(subMtx,col=colors, main=title, xlab=xlabel, ylab=ylabel,las=3)

お遊戯1

plot(0,0,pch=8)

plot(0,0,pch=8,cex=5)

plot(0,0,pch=8,cex=5,col="red")

manipulate package

インタラクティブなプロット

manipulate packageがインストールされていない場合

install.packages("manipulate")
library(manipulate)

お遊戯編:色の選択

picker()関数

manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") )

お遊戯編:プロットマーカーの選択

picker()関数

manipulate(
  plot(0,0,pch=myMarkers,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan",initial="violet"),
myMarkers=picker(1,2,3,4,5,6,7,8,initial="5")
)

お遊戯編:プロットサイズの選択

picker()関数

manipulate(
  plot(0,0,pch=8,cex=mySize,col="blue"),
mySize=slider(1,10,initial=5)
)

お遊戯2

plot(0,0,type="n")
text(0,0, "R",cex=1,col="blue")

お遊戯編:色の選択picker()関数

manipulate関数のメイン文は{}で囲む

manipulate({
  plot(0,0,type="n")
  text(0,0, "R",cex=1,col=myColors)
}, 
  myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") )

練習:プロットサイズの選択

slider関数: 最小=1,最大=10, 初期値3

alt text

alt text

実践編

実践編1(barplot): 色の選択(初期値の色=“pink”)

barplot(subMtx,col="red",main=title, xlab=xlabel, ylab=ylabel,las=3)

alt text

alt text

実践編2(barplot): スライダーの追加

スライダー:最小1, 最大20, 初期値10

barplot(subMtx,col="green",main=title, xlab=xlabel, ylab=ylabel, xlim=c(1,20),las=3)

alt text

alt text