如何在多边形中获得文字云

时间:2014-09-06 06:34:54

标签: r polygon data-visualization word-cloud

我搜索了几个小时来解决这个问题 - 有很多输入,但还没有正确的解决方案。我希望有人可以帮助我。

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

我所拥有的是两种主要方法:

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

    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)的情节中得到了两个人。结果到目前为止:

    enter image description here

    正如您所看到的,单词在我的多边形中不匹配。我的问题是我没有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方法:文字

    我使用help of this来了以下代码:

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

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

    enter image description here

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

    我希望我的一个方法可以从你的一个R专业人员那里修复。

    提前感谢您的帮助!如果您需要更多信息,请告诉我们!

0 个答案:

没有答案