sentence1 & sentence1
sentence1 <- "The parent of Facebook, Instagram and WhatsApp reduced its work force by 13 percent and extended a hiring freeze through the first quarter of next year."
sentence2 <- "Over the summer, Lyft cut 2 percent of its employees, mostly as a result of shutting down its car rental business, and froze hiring. "
sentence1出現頻度表
wordLst<-strsplit(sentence1,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
s1_freq <- table(wordLst)
(s1_freq_table<-sort(s1_freq, decreasing=TRUE))
wordLst
and of the 13 a by extended facebook first
2 2 2 1 1 1 1 1 1
force freeze hiring instagram its next parent percent quarter
1 1 1 1 1 1 1 1 1
reduced through whatsapp work year
1 1 1 1 1
sentence2出現頻度表
wordLst<-strsplit(sentence2,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
s2_freq <- table(wordLst)
(s2_freq_table<-sort(s2_freq, decreasing=TRUE))
wordLst
its of 2 a and as business car cut
2 2 1 1 1 1 1 1 1
down employees froze hiring lyft mostly over percent rental
1 1 1 1 1 1 1 1 1
result shutting summer the
1 1 1 1
2つの変数を連結(merge)
全ての単語
freq_merge <-merge(s1_freq_table, s2_freq_table, all=T, by="wordLst",suffixes = c(".s1",".s2"))
freq_merge
共通単語のみ
merge(s1_freq_table, s2_freq_table, by="wordLst")
s1_freq_tableの単語基準
merge(s1_freq_table, s2_freq_table, all.x=TRUE, by="wordLst")
s2_freq_tableの単語基準
merge(s1_freq_table, s2_freq_table, all.y=TRUE, by="wordLst")
”sample_ja_2”の頻度テーブル
txt2<-readLines("sample_texts/sample_ja_2.txt")
wordLst<-strsplit(txt2,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<- wordLst[wordLst != ""]
freq2 <- table(wordLst)
freq_table2<-sort(freq2, decreasing=TRUE)
(freqData2 <- data.frame(freq_table2))
wordcloud(freqData2$wordLst,freqData2$Freq)

binfo<-brewer.pal.info[]
palets <-rownames(binfo[binfo$maxcolors>10,])
palets
[1] "BrBG" "PiYG" "PRGn" "PuOr" "RdBu" "RdGy" "RdYlBu" "RdYlGn"
[9] "Spectral" "Paired" "Set3"
wordcloud using custom colors
palets[10]
[1] "Paired"
wordcloud(freqData2$wordLst,freqData2$Freq, colors=brewer.pal(10,palets[1]))

wordcloud using custom colors
wordcloud(freqData2$wordLst,freqData2$Freq, colors=brewer.pal(10,palets[10]), min.freq=2)

manipulate (インタラクティブなプロット)
manipulate関数の書き方
manipulate(
実行スクリプト,
picker, sliderの情報(複数の場合はカンマで結合)
)
manipulate(
{
複数の実行スクリプト
},
picker, sliderの情報(複数の場合はカンマで結合)
)
注意:manipulate関数は、配布Notebookファイルではなく、拡張子”.R”のファイル上で確認すること
色の選択
title="PCH Symbols"
xlabel="x"
ylabel="y"
plot(0,0,pch=8,cex=5,col="blue", main=title, xlab=xlabel, ylab=ylabel)

manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan") )
colors = c("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan")
manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker(colors) )
プロットマーカーの選択
manipulate(
plot(0,0,pch=myMarkers,cex=5,col=myColors), myColors=picker("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan",initial="violet"),
myMarkers=picker(1,2,3,4,5,6,7,8,initial="5")
)
picker + List
colPalets = c("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan")
manipulate(
plot(0,0,pch=myMarkers,cex=5,col=myColors, main=title, xlab=xlabel, ylab=ylabel),
myColors=picker(as.list(colPalets),initial=colPalets[2]),
myMarkers=picker(as.list(seq(1,8)),initial="5")
)
プロットサイズの選択
manipulate(
plot(0,0,pch=8,cex=mySize,col="blue"),
mySize=slider(1,10,initial=5)
)
色付き棒グラフ
color8 = c("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan")
barplot(freqLst, las=3,col=color8,ylab="Frequency")
