2014-01-05 23 views
1

我目前正在研究比較英國國會議員在議會中的角色和他們在Twitter上的角色。我從一位議員收集了Twitter數據(最重要的是原始文本)和議會發言,並希望做一個散點圖,顯示Twitter和議會(右上角)哪些詞是常見的,哪些詞不是(左下角手角落)。所以,x軸是議會中的詞頻,y軸是twitter上的詞頻。R中的詞頻散點圖(詞作爲標籤)

到目前爲止,我已經用R完成了本文的所有工作。我對R有零經驗,直到現在我只與STATA合作過。

我試着調整這個代碼(http://is-r.tumblr.com/post/37975717466/text-analysis-made-too-easy-with-the-tm-package),但我不能解決它。主要問題是編寫此代碼的人使用一個文本文檔和正則表達式來劃分哪些文本屬於哪個軸。然而,我有兩個單獨的文檔(我將它們保存爲.txt,corpi或term-document-matrices),它們應該對應於單獨的軸。

對不起,像我這樣的新手正在爲此煩惱,今年我會投入更多的時間來學習R的基礎知識,以便我可以自己解決這個問題。但是,這篇論文將於下週一發布,我現在不能做太多的回溯來解決問題。

我會很感激,如果你能幫助我,

非常感謝,

編輯:我會在我所做的代碼,即使它是不太正確的方向,但這樣我可以提供我正在處理的一個適當的例子。

我已經嘗試通過在csv文件中使用文本問題來實現is.R()s方法,用虛擬變量來分類它是否是twitter文本或語音文本。我遵循這個方法,最後我甚至得到一個散點圖,但是,它繪製了數字(我認爲它是數據集中單詞所在的數字??)而不是單詞。我認爲問題可能是R將csv文件中的每一行都作爲單獨的文本文檔處理。

# in excel i built a csv dataset that contains all the text, each instance (single tweet/speech) in one line, with an added dummy variable that clarifies whether the text is a tweet or a speech ("istweet", 1=twitter). 

comparison_watson.df <- read.csv(file="data/watson_combo.csv", stringsAsFactors = FALSE) 

# now to make a text corpus out of the data frame 

comparison_watson_corpus <- Corpus(DataframeSource(comparison_watson.df)) 
inspect(comparison_watson_corpus) 

# now to make a term-document-matrix 

comparison_watson_tdm <-TermDocumentMatrix(comparison_watson_corpus) 
inspect(comparison_watson_tdm) 

comparison_watson_tdm <- inspect(comparison_watson_tdm) 
sort(colSums(comparison_watson_tdm)) 
table(colSums(comparison_watson_tdm)) 

termCountFrame_watson <- data.frame(Term = rownames(comparison_watson_tdm)) 
termCountFrame_watson$twitter <- colSums(comparison_watson_tdm[comparison_watson.df$istwitter == 1, ]) 
termCountFrame_watson$speech <- colSums(comparison_watson_tdm[comparison_watson.df$istwitter == 0, ]) 

head(termCountFrame_watson) 

zp1 <- ggplot(termCountFrame_watson) 
zp1 <- zp1 + geom_text(aes(x = twitter, y = speech, label = Term)) 
print(zp1) 
+1

那麼如何生成頻率數據或者如何繪製頻率數據呢? –

+0

這個問題將是如何繪製它。 – nikloynes

+0

情節(x,y)有什麼問題。你的問題到底是什麼? – ECII

回答

0

您不是發佈一個可重複的例子,所以我不能給你代碼,但只能找到你的資源。 R的文本拼寫和處理有點困難,但有很多指南。檢查thisthis。在最後的步驟中,你可以得到字數。

在這個例子中從One R Tip A Day你在d$word單詞列表,並在d$freq

+1

您好,感謝您的幫助。我實際上已經使用了你給我做的例子來做一個worcloud。我無法推斷出如何做一個散點圖形式,雖然...添加了一些代碼來完成,所以也許我想做的事情變得更清晰。抱歉用我不清楚的要求打擾你們。 – nikloynes

3
library(tm) 
txts <- c(twitter="bla bla bla blah blah blub", 
      speech="bla bla bla bla bla bla blub blub") 
corp <- Corpus(VectorSource(txts)) 
term.matrix <- TermDocumentMatrix(corp) 
term.matrix <- as.matrix(term.matrix) 
colnames(term.matrix) <- names(txts) 
term.matrix <- as.data.frame(term.matrix) 

library(ggplot2) 
ggplot(term.matrix, 
     aes_string(x=names(txts)[1], 
        y=names(txts)[2], 
        label="rownames(term.matrix)")) + 
    geom_text() 

你可能也想嘗試這兩個哥們詞頻:

library(wordcloud) 
comparison.cloud(term.matrix) 
commonality.cloud(term.matrix) 

enter image description here

+0

嘿,那裏,非常感謝,它已經奏效! – nikloynes