How can we show the boundary of a WordCloud shape?

15

6

WordCloud can arrange the words inside a shape.

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

Mathematica graphics

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

enter image description here

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"].

Szabolcs

Posted 2015-07-22T10:23:20.247

Reputation: 213 047

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 – 2015-07-22T11:17:05.263

@gpap But shouldn't they still fit inside of the shape, even though they can't follow the boundary very closely? – Szabolcs – 2015-07-22T11:57:30.443

@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.

– halirutan – 2015-07-22T12:30:24.343

Answers

18

shape = CountryData["Ethiopia", "Shape"]; 
mask =  ColorNegate@Binarize[shape, .99];

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]
   }
  ]
 ]

Mathematica graphics

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];

Mathematica graphics

Sjoerd C. de Vries

Posted 2015-07-22T10:23:20.247

Reputation: 63 549

1A wholehearted +1 from me. Syria seems to like being engulfed by China's "C". – gpap – 2015-07-22T12:51:31.013

Thanks. I gather you used the shape directly in the word cloud function? At least that's what I did initially. – Sjoerd C. de Vries – 2015-07-22T12:56:06.607

1Yes, I did exactly that and gave up :) – gpap – 2015-07-22T13:02:53.007