Make it a function. Try other character names:
This expression can be written more concisely using a pure function so that the parts of the Legended subexpressions that are similar are not duplicated:
SmoothHistogram[{Legended[
First /@
StringPosition[ExampleData[{"Text", "AliceInWonderland"}],
"Alice"], "Alice"],
Legended[
First /@
StringPosition[ExampleData[{"Text", "AliceInWonderland"}],
"Queen"], "Queen"]}, Filling -> Axis]
The only parts of the Legended subexpressions that differ are the names “Alice” and “Queen”. Replace them with # in a pure function (indicated by &) that is applied to a list of the names:
SmoothHistogram[
Legended[First /@
StringPosition[
ExampleData[{"Text",
"AliceInWonderland"}], #], #] & /@ {"Alice", "Queen"},
Filling -> Axis]
This form of the expression is easy to turn into a function that will work with any number of names. Replace the names list with the variable names and define the function storyflowinalice with the parameter names:
storyflowinalice[names_] :=
SmoothHistogram[
Legended[First /@
StringPosition[
ExampleData[{"Text", "AliceInWonderland"}], #], #] & /@ names,
Filling -> Axis]
Test the function on three character names:
storyflowinalice[{"Alice", "Queen", "Hatter"}]
Run this first to define the function:
storyflowinalice[names_] :=
SmoothHistogram[
Legended[First /@
StringPosition[
ExampleData[{"Text", "AliceInWonderland"}], #], #] & /@ names,
Filling -> Axis]
Plot the story flow using the function:
storyflowinalice[{"Alice", "Queen", "Hatter"}]