# How can we show the boundary of a WordCloud shape?

WordCloud can arrange the words inside a shape.

data = EntityValue[CountryData[], {"Name", "Population"}];
WordCloud[data, Disk[], WordOrientation -> "Random", Frame -> True]


As you can see from the plot range, the coordinates in the final word cloud don't seem to be related to the coordinates in the original shape specification. This is true regardless of whether the shape is specified as an image, graphics or region.

I would like to show the word cloud together the original shape. E.g. something like this (manually positioned):

I need to do this not for a disk/circle but any complex arbitrary shape. How can I achieve this? Is there any way to retrieve the mapping between the coordinates of the original shape and resulting word cloud?

Here's a random complex shape to test on: CountryData["Ethiopia", "Shape"].

• I think there's an issue with using a shape beyond the matching of coordinates. Try a complicated country shape like Italy or Greece and you'll see that the words are not at all resembling the shape of the resp. country. This might have to do with the weights and the scaling of course but still...
– gpap
Jul 22, 2015 at 11:17
• @gpap But shouldn't they still fit inside of the shape, even though they can't follow the boundary very closely? Jul 22, 2015 at 11:57
• @Szabolcs Making the word cloud having the shape is easy. To see whether we can work in the original shapes dimension, I have to inspect this further. Jul 22, 2015 at 12:30

shape = CountryData["Ethiopia", "Shape"];

data = EntityValue[CountryData[], {"Name", "Population"}];
wc = WordCloud[data, mask, WordOrientation -> "Random", Frame -> False];

shapeRange = PlotRange /. AbsoluteOptions[shape, PlotRange];
cloudRange = PlotRange /. AbsoluteOptions[wc, PlotRange];

tr = FindGeometricTransform[cloudRange\[Transpose], shapeRange\[Transpose]][[2]];

Show[
wc,
Graphics[
{
EdgeForm[Black], FaceForm[],
GeometricTransformation[shape[[1, 3]], tr]
}
]
]


Since gpap reported problems with Italy here's Italy. I increased the image size of the bitmapped mask a bit to catch more of the complex coastal shape using:

shape = CountryData["Italy", "Shape"];
mask = ColorNegate@Binarize[Image[shape, ImageSize -> 1000], .99];


• A wholehearted +1 from me. Syria seems to like being engulfed by China's "C".
– gpap
Jul 22, 2015 at 12:51
• Thanks. I gather you used the shape directly in the word cloud function? At least that's what I did initially. Jul 22, 2015 at 12:56
• Yes, I did exactly that and gave up :)
– gpap
Jul 22, 2015 at 13:02