Issues in Wordcloud2 package

Does anyone know why wordcloud2 just allow only 1 wordcloud2 html in R markdown

I try to use wordcloud2 function twice in R markdown. But there is just the first one is valid when I knitr to html_document.

1 Like

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

Hi there

There are code from my kaggle notebook.

In my question, I mean the output just shows for me only 1 wordcloud2 html format although I use wordcloud2 function many times

---
title: "Text_mining_cartoon"
author: "Nguyen_LSCM"
date: "April 15 2020"
output: 
flexdashboard::flex_dashboard:
               vertical_layout: scroll
               theme: yeti
---




Hi I am just a second year student. I learn these great things by myself, so please to give me more suggestions if you find any problems with my kernel. 
Also, Please to upvote if you like my kernel

``` {js}
// Inverse color of navigation bar.
$('.navbar-inverse').removeClass('navbar-inverse').addClass('navbar-default');
```
```{r, include=FALSE}
library(tidyverse)
library(tidyr)
library(tidytext)
library(widyr)
library(scales)
library(wordcloud2)
library(wordcloud)
library(RColorBrewer)
library(rjson)
library(reshape2)
library(purrr)
library(plotly)
```

```{r include=FALSE}
txt<-read.csv("../input/RickAndMortyScripts.csv",header=TRUE)

colnames(txt)<-c("index","season","episode.code","episode.name","name","line")
```

<style>
.colored {
  background-color: #FAFAFA;
}
</style>


Overview 
=======================================================

Column {.tabset .tabset-fade data-width=500 .colored }
-----------------------------------------------------------------------

### Wordcloud {.no-padding}

```{r}

txt_pr<- txt%>% mutate(line=as.character(line))%>%unnest_tokens(output = word,input=line)%>%anti_join(stop_words)%>%filter(!word %in% c("iâ","hey","donâ","â","â"))

txt_gam_count<-txt_pr%>%group_by(word)%>%summarise(freq=n())

m<-wordcloud2(txt_gam_count,size=12,backgroundColor = "lightgreen",shape="star")

m
m
```

### Sentiment {.no-padding}

```{r}

txt_pr<-txt%>% mutate(line=as.character(line))%>%unnest_tokens(output = word,input=line)%>%group_by(word)%>%count(sort=TRUE)%>%inner_join(get_sentiments("bing"))
txt_pr%>%acast(word~sentiment,value.var="n",fill=0)%>%comparison.cloud(title.size = 4,title.colors = c("red","blue"),max.words = 200,colors = brewer.pal(6,"Set1"),match.colors = TRUE,title.bg.colors = "white")
```

Column {.tabset .tabset-fade data-width=500 .colored }
-----------------------------------------------------------------------

### Three seasons {.no-padding}

```{r}
txt_pr<- txt%>% mutate(line=as.character(line))%>%unnest_tokens(output = word,input=line)%>%anti_join(stop_words)%>%filter(!word %in% c("iâ","hey","donâ","â","â"))

txt_gam_count<-txt_pr%>%group_by(season,word)%>%summarise(freq=n())

 txt_gam_count%>%ungroup()%>%mutate(season=fct_recode(factor(season),"season 1"="1","season 2"="2","season 3"="3"))%>%acast(word~season,value.var = "freq",fill=0)%>%comparison.cloud(random.order=FALSE, 
colors = c("#00B2FF",  "#FF0099", "#6600CC"),
title.size=1.5, max.words=300,title.colors = c("#00B2FF",  "#FF0099", "#6600CC"))
```

### Bar_plot for each season {.no-padding}

```{r}
bar<- list()

txt_pr<- txt%>% mutate(line=as.character(line))%>%unnest_tokens(output = word,input=line)%>%anti_join(stop_words)%>%filter(!word %in% c("iâ","hey","donâ","â","â"))


bar_plot<-function(number){
          bar<- txt_pr%>%filter(season==number)%>%group_by(season)%>%count(word,sort=TRUE)%>%head(15)
}
bar<-map(c(1,2,3),bar_plot)

df<-bind_rows(bar[1],bar[2],bar[3])%>%mutate(word=str_to_title(fct_reorder2(word,season,desc(n))))

gg<-df%>%ggplot(aes(x=fct_reorder2(word,season,desc(n)),y=n))+geom_bar(stat="identity",aes(fill=as.factor(season)))+theme_minimal()+scale_fill_manual(values=c("red","green","steelblue"))+facet_wrap(~season,nrow=3,scales = "free")+labs(title="Top words in each season","Scale_x_with_log10")+xlab("Number of frequency")+ylab("Word")+coord_flip()+scale_y_log10()+theme(legend.title=element_blank())

ggplotly(gg)
```

Character
========================================================================

Column {data-width=250}
-----------------------------------------------------------------------


### Rick scrips {.no-padding}

```{r}

txt_pr<- txt%>% mutate(line=as.character(line))%>%unnest_tokens(output = word,input=line)%>%anti_join(stop_words)%>%filter(!word %in% c("iâ","hey","donâ","â","â"))%>%filter(name %in% c("Rick","Morty","Beth","Jerry"))

txt_gam_count<-txt_pr%>%filter(name=="Rick")%>%group_by(word)%>%summarise(freq=n())

m<-wordcloud2(txt_gam_count,size = 12,backgroundColor = "pink",color = "white",shape = "star")

m
m
```

Column {data-width=250}
-----------------------------------------------------------------------

### Morty scrips {.no-padding}
```{r}


txt_gam_count<-txt_pr%>%filter(name=="Morty")%>%group_by(word)%>%summarise(freq=n())

m<-wordcloud2(txt_gam_count,size = 12,backgroundColor = "lightred",color = "white",shape = "circle")

m
m
```


Column {data-width=250}
-----------------------------------------------------------------------


### Beth scrips {.no-padding}
```{r}
txt_gam_count<-txt_pr%>%filter(name=="Beth")%>%group_by(word)%>%summarise(freq=n())

m<-wordcloud2(txt_gam_count,size = 12,backgroundColor = "lightblue",color = "white",shape = "triangle")

m
m
```

Column scrips{data-width=250}
-----------------------------------------------------------------------

### Jerry {.no-padding}

```{r}
txt_gam_count<-txt_pr%>%filter(name=="Jerry")%>%group_by(word)%>%summarise(freq=n())

m<-wordcloud2(txt_gam_count,size = 12,backgroundColor = "lightorchid",color = "white",shape = "diamond")

m
m
```

Hi NguyenLSCM,

I had the same issue with a Shiny app I was working on a while back so I understand your frustration. I'm not 100% sure why only one plot is output but the issue may be with the way wordcloud2 interacts with the wordcloud2.js library.

If you look here, you'll notice that this has been an issue for almost two years. There may be a workaround by updating the wordcloud.js library to the newest version - check here.

It's also possible that the CRAN version, 0.2.1, is the problem and that a newer version, 0.2.2 and up, may work. One of the author's collaborators seems to think this will solve the problem. However, I am skeptical since there have been literally no code changes for over 2 years but...hey, it's possible.

If you can't get it to work, then I suggest using a non-interactive library such as ggwordcloud or something similar - not as cool but still gets the point across.

Good luck!

-Joon

1 Like

Yay, you are a good one.
Thank you very much.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.