1

我花了好几个小时寻找这个问题的解决方案——投入了很多,但还没有找到正确的解决方案。我希望有人可以帮助我。

我的目标: 我的 wordcloud 适合定义的多边形(带有 x 和 y 坐标)。

我有两种主要方法:

  1. 方法:尝试使用 R 包wordcloud

我有:

a) 完成的wordcloud

wordcloud(my_wordcloud, scale=c(4,0.5), max.words=500, min.freq="2", random.order=FALSE, rot.per=0.05, use.r.layout=FALSE,colors=brewer.pal(6,"Dark2"))

b) 定义的多边形

plot(my_coordinates, type="l",  axes = F, xlab = NA, ylab = NA)

现在我在一个情节中与par(new=TRUE). 到目前为止的结果是:

在此处输入图像描述

如您所见,我的多边形中的单词不匹配。我的问题是我没有迹象表明 ; 的周边区域wordcloud。我不知道在哪里传递我的多边形的坐标。

我的第一种方法的代码:

#librarys
library("SnowballC", lib.loc="~/R/i686-pc-linux-gnu-library/3.1")
library("tm", lib.loc="~/R/i686-pc-linux-gnu-library/3.1")
library("wordcloud", lib.loc="~/R/i686-pc-linux-gnu-library/3.1")

#Corpus (txt-file)
corp <- Corpus (DirSource("in/test"))
corp <- tm_map(corp, stripWhitespace)
corp <- tm_map(corp, tolower)
corp <- tm_map(corp, removeWords, stopwords("german"))
corp <- tm_map(corp, stripWhitespace)
corp <- tm_map(corp, removePunctuation)
#make a txt-file
corp_clean <- tm_map(corp, PlainTextDocument)

#the coords as csv (columns: x, y)
ch <- read.csv(file="coord/cord_ch.csv", sep = ";")

# Wordcloud
wordcloud(corp_clean, scale=c(4,0.5), max.words=100, min.freq="3", random.order=FALSE, rot.per=0.05, use.r.layout=FALSE,colors=brewer.pal(6,"Dark2"))
# new Plot
par(new=TRUE)

#plot the coords
plot(ch, type="l", axes = F, xlab = NA, ylab = NA, ylim = rev(range(ch$y)))

现在我的第二种方法:

2 方法:文字

在此的帮助下,我得出了以下代码:

#after some steps from my first approach (we begin with #make a txt-file from above code)
corp_clean <- tm_map(corp, PlainTextDocument)
# some definings
myTdm <- TermDocumentMatrix(corp_clean)
termFrequency <- rowSums(as.matrix(myTdm))
termFrequency <- subset(termFrequency, termFrequency>=3)
df <- data.frame(term=names(termFrequency), freq=termFrequency)
m <- as.matrix(myTdm)
wordFreq <- sort(rowSums(m), decreasing=TRUE)
#plot the coordinates
plot(ch, type="l", axes = F, xlab = NA, ylab = NA, ylim = rev(range(ch$y)))
par(new=TRUE)
#plot the text (wordcloud) by telling the coordinates
text(x = ch$x, y = ch$y,labels = names(wordFreq))

使用该代码,结果图如下所示:

在此处输入图像描述

这里我们有坐标位置的单词。是否可以将坐标声明为文本的周边区域?

我希望我的一种方法可以从你们中的一位 R 专业人士那里得到解决。

在此先感谢您的帮助!如果您需要更多内容,请告诉我!

4

0 回答 0