Situation
I'm using the Vega visualization grammar tool in Looker Studio. Specifically, the wordcloud chart.
Documentation: https://vega.github.io/vega/docs/transforms/wordcloud/
What I've tried / Results
The word clouds I'm generating have too many words in them, and I'm trying to figure out how to reduce that.
Manipulating the example editor on that page, I can sort of get the result I want by setting the 'wordpadding' to 5, but this seems to just push some of the words out of view, versus reduce the number of words and then distributing those words across the size spectrum.
I've also tried changing the 'padding' in the Style Properties in Looker studio, but it's not changing anything about the chart.
Any input on this would be greatly appreciated.
Updating with current code
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"data": [
{
"name": "default",
"transform": [
{
"type": "formula", "as": "rotate",
"expr": "[0, 90][~~(datum.index % 2)]"
},
{
"type": "formula", "as": "weight",
"expr": "if(datum.index==0, 600, 400)"
},
{
"type": "wordcloud",
"size": [{"signal": "width"}, {"signal": "height"}],
"text": {"field": "$dimension0"},
"fontSize": {"field": "$metric0"},
"fontWeight": {"field": "weight"},
"fontSizeRange": [{"signal": "(width+height)/96"}, {"signal": "(width+height)/24"}],
"padding": {"value": 2},
"rotate": {"field": "rotate"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "default", "field": "$dimension0"},
"scheme": "datastudio20"
}
],
"marks": [
{
"type": "text",
"from": {"data": "default"},
"encode": {
"enter": {
"text": {"field": "$dimension0"},
"align": {"value": "center"},
"baseline": {"value": "alphabetic"},
"fill": {"scale": "color", "field": "$dimension0"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"angle": {"field": "angle"},
"fontSize": {"field": "fontSize"},
"fontWeight": {"field": "weight"},
"fillOpacity": {"value": 0.7}
},
"hover": {
"fillOpacity": {"value": 1}
}
}
}
]
}
What I tried based on that
@Davide
From there, I found this line in your code:
{"type": "filter", "expr": "datum.row < 25"},
So I tried adding that under the "transform" section in my code.
Result
However, that only produced a blank chart.
Updated code with both 'window' and 'filter' transform
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"data": [
{
"name": "default",
"transform": [
{
"type": "formula", "as": "rotate",
"expr": "[0, 90][~~(datum.index % 2)]"
},
{
"type": "formula", "as": "weight",
"expr": "if(datum.index==0, 600, 400)"
},
{
"type": "formula",
"as": "weight",
"expr": "if(datum.text=='VEGA', 600, 300)"
},
{
"type": "formula",
"as": "rotate",
"expr": "[-rotate, 0, rotate][~~(random() * 3)]"
},
{
"type": "window",
"sort": {"field": "count", "order": "descending"},
"ops": ["row_number"],
"fields": [null],
"as": ["row"]
},
{"type": "filter", "expr": "datum.row < 25"},
{
"type": "wordcloud",
"size": [{"signal": "width"}, {"signal": "height"}],
"text": {"field": "text"},
"font": "Helvetica Neue, Arial",
"fontSize": {"field": "count"},
"fontWeight": {"field": "weight"},
"fontSizeRange": [
{"signal": "fontSizeRange0"},
{"signal": "fontSizeRange1"}
],
"padding": {"signal": "wordPadding"},
"rotate": {"field": "rotate"}
},
{
"type": "wordcloud",
"size": [{"signal": "width"}, {"signal": "height"}],
"text": {"field": "$dimension0"},
"fontSize": {"field": "$metric0"},
"fontWeight": {"field": "weight"},
"fontSizeRange": [{"signal": "(width+height)/96"}, {"signal": "(width+height)/24"}],
"padding": {"value": 2},
"rotate": {"field": "rotate"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "default", "field": "$dimension0"},
"scheme": "datastudio20"
}
],
"marks": [
{
"type": "text",
"from": {"data": "default"},
"encode": {
"enter": {
"text": {"field": "$dimension0"},
"align": {"value": "center"},
"baseline": {"value": "alphabetic"},
"fill": {"scale": "color", "field": "$dimension0"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"angle": {"field": "angle"},
"fontSize": {"field": "fontSize"},
"fontWeight": {"field": "weight"},
"fillOpacity": {"value": 0.7}
},
"hover": {
"fillOpacity": {"value": 1}
}
}
}
]
}
Code that worked
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"data": [
{
"name": "default",
"transform": [
{
"type": "countpattern",
"field": "$dimension0",
"pattern": "[\\w']{3,}",
"stopwords": "very|now|can't|are|800|every|also|ever|just|dont|don't|been|pnly|I've|I'm|you|why|try|but|was|it's|her|2021|where|com|not|for|that|from|and|out|this|the|has|have|2022|2021"
},
{
"type": "formula", "as": "weight",
"expr": "log(datum.count)*5"
},
{
"type": "window",
"sort": {"field": "count", "order": "descending"},
"ops": ["row_number"],
"fields": [null],
"as": ["row"]
},
{"type": "filter", "expr": "datum.row < 40"},
{
"type": "formula", "as": "rotate",
"expr": "[0, 90][~~(datum.count % 2)]"
},
{
"type": "wordcloud",
"size": [{"signal":"width"}, {"signal":"height"}],
"text": {"field": "text"},
"font": "Helvetica Neue",
"fontSize": {"field": "count"},
"fontWeight": {"field": "weight"},
"fontSizeRange": [
{"signal": "(width+height)/96"},
{"signal": "(width+height)/24"}],
"rotate": {"field": "rotate"},
"padding": 2
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "default", "field": "text"},
"scheme": "set3"
}
],
"marks": [
{
"type": "text",
"from": {"data": "default"},
"encode": {
"enter": {
"text": {"field": "text"},
"align": {"value": "center"},
"baseline": {"value": "alphabetic"},
"fill": {"scale": "color", "field": "text"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"angle": {"field": "angle"},
"fontSize": {"field": "fontSize"},
"fontWeight":{"field": "weight"},
"fillOpacity": {"value": 0.6}
},
"hover": {
"fillOpacity": {"value": 1}
}
}
}
]
}
This example filters the top 25 words by count.