Wordcloud is cropping text
Asked Answered
M

3

14

I am using twitter API to generate sentiments. I am trying to generate a word-cloud based on tweets.

Here is my code to generate a wordcloud

wordcloud(clean.tweets, random.order=F,max.words=80, col=rainbow(50), scale=c(3.5,1))

Result for this:

enter image description here

I also tried this:

pal <- brewer.pal(8,"Dark2")

wordcloud(clean.tweets,min.freq = 125,max.words = Inf,random.order  = TRUE,colors = pal)

Result for this:

enter image description here

Am I missing something?

This is how I am getting and cleaning tweets:

#downloading tweets
tweets <- searchTwitter("#hanshtag",n = 5000, lang = "en",resultType = "recent")
# removing re tweets 
no_retweets <- strip_retweets(tweets , strip_manual = TRUE)

#converts to data frame
df <- do.call("rbind", lapply(no_retweets , as.data.frame))

#remove odd characters
df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub="")) #remove emoticon
df$text = gsub("(f|ht)tp(s?)://(.*)[.][a-z]+", "", df$text) #remove URL
sample <- df$text


    # Cleaning Tweets 
    sum_txt1 <- gsub("(RT|via)((?:\\b\\w*@\\w+)+)","",sample)
    sum_txt2 <- gsub("http[^[:blank:]]+","",sum_txt1)
    sum_tx3 <- gsub("@\\w+","",sum_txt2)
    sum_tx4 <- gsub("[[:punct:]]"," ", sum_tx3)
    sum_tex5 <- gsub("[^[:alnum:]]", " ", sum_tx4)
    sum_tx6 <- gsub("RT  ","", sum_tex5)

    # WordCloud

    # data frame is not good for text convert it corpus
    corpus <- Corpus(VectorSource(sum_tx6))
    clean.tweets<- tm_map(corpus , content_transformer(tolower)) #converting everything to lower cases
    clean.tweets<- tm_map(guj_clean,removeWords, stopwords("english")) #stopword are words like of, the, a, as..
    clean.tweets<- tm_map(guj_clean, removeNumbers)
    clean.tweets<- tm_map(guj_clean, stripWhitespace)

Thanks in advance!

Melodize answered 28/11, 2017 at 5:31 Comment(2)
Can you share the code you used to create the clean.tweet object ? I have no pblm producing a wordcloud on my machine. Any chance you've specified a par() option before?Feticide
@ColinFAY please check updated question. I am not using par() in my codeMelodize
S
3

The cropping seems to happen when there are previous plots in the history. I ran into this issue as well and was able to fix mine by clicking the broom button to clear plot history (click the link to see the image), and then recreating the Wordcloud. view image here.

Sneed answered 14/11, 2022 at 22:40 Comment(2)
It would be useful to provide code to show exactly how you managed to solve the issue with the word cloud.Hypothermal
I didn't solve it with code, I used the same code as the previous answer to generate the wordcloud, and then fixed the cropping issue by clearing the plot history in R, and then re-generated the wordcloud. I added a link to an image of which button I pushed to fix the cropping.Sneed
S
2

Try changing the scale on your wordcloud from c(3.5,1) to c(3.5,0.25).

wordcloud(clean.tweets, random.order=F,max.words=80, col=rainbow(50), scale=c(3.5,0.25))
Sherard answered 9/12, 2017 at 4:58 Comment(0)
L
0

I had the same problem. I used dev.off() just before plotting the cloud and it worked for me.

Landsturm answered 21/3 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.