Wolfram Research

Function Repository Resource:

CheckboxLegended

Source Notebook

Add a legend with checkboxes to a plot that toggles individual datasets on and off dynamically

Contributed by: Sjoerd Smit

ResourceFunction["CheckboxLegended"][func[<|lbl1 item1,lbl2item2,|>,]]

returns the plot generated by func with plot legends that include checkboxes for showing and hiding individual plotted items.

ResourceFunction["CheckboxLegended"][func[],Dynamic[var]]

links the visibility of the datasets to variable var.

Details and Options

Typical values of usable plotting functions, func, are ListPlot, BubbleChart and Plot.
ResourceFunction["CheckboxLegended"] works by wrapping custom Legended and Style wrappers around itemi before evaluating func.
ResourceFunction["CheckboxLegended"] does not check which plotting function is being used, so it may give unexpected results for plotting functions (such as ListContourPlot) that do not support multiple datasets.
Some plotting functions (such as ListPlot3D) do not support dataset wrappers of the form Style[data, Opacity[Dynamic[var]]] and will not work with ResourceFunction["CheckboxLegended"].
ResourceFunction["CheckboxLegended"] has the HoldFirst attribute and will evaluate the data in the plotting function to test if it’s an association.
When used with plotting functions with a Hold attribute like Plot, ResourceFunction["CheckboxLegended"] will evaluate the first argument of the plotting function. This means that the localization of the arguments will not work correctly.
When you specify your own linking variable Dynamic[var], ResourceFunction["CheckboxLegended"] will return a Graphics (or Graphics3D) object that can be combined with other graphics.

Examples

Basic Examples

Add a Checkbox legend to a ListPlot:

In[1]:=
dataset = <|"Dataset1" -> Range[10], "Dataset2" -> Range[10, 1, -1]|>;
ResourceFunction["CheckboxLegended"][
 ListPlot[dataset]
 ]
Out[2]=

The options of ListPlot can still be used to specify your own styles:

In[3]:=
ResourceFunction["CheckboxLegended"][
 ListPlot[dataset, PlotStyle -> {Red, Blue}]
 ]
Out[3]=

Use it with Plot:

In[4]:=
ResourceFunction["CheckboxLegended"][
 Plot[<|HoldForm[x] -> x, HoldForm[x^2] -> x^2, HoldForm[Sin[x]] -> Sin[x] |>, {x, 0, Pi}]
 ]
Out[4]=

With BubbleChart:

In[5]:=
ResourceFunction["CheckboxLegended"][
 BubbleChart[
  AssociationThread[Range[Length[#]], #] &@RandomReal[1, {5, 7, 3}]
  ]
 ]
Out[5]=

Link the visibility of datasets to a variable:

In[6]:=
DynamicModule[{plotActive},
 Column[{
   ResourceFunction["CheckboxLegended"][
    BubbleChart[
     AssociationThread[Range[Length[#]], #] &@
      RandomReal[1, {5, 7, 3}]
     ],
    Dynamic[plotActive]
    ],
   Dynamic[plotActive]
   }
  ]
 ]
Out[6]=

Scope

Other data wrappers will remain functional:

In[7]:=
Clear[x];
ResourceFunction["CheckboxLegended"][
 Plot[<|HoldForm[x] -> Callout[x, "x", Above], HoldForm[x^2] -> Style[x^2, Red] |>, {x, 0, 1}]
 ]
Out[8]=

Options

Use the "LegendLayoutFunction" option to style the legend:

In[9]:=
ResourceFunction["CheckboxLegended"][
 BubbleChart[
  AssociationThread[Range[Length[#]], #] &@RandomReal[1, {5, 7, 3}]
  ],
 "LegendLayoutFunction" -> Function[{checkbox, label}, Framed[Column[{label, checkbox }]]]
 ]
Out[9]=

Possible Issues

Normally, Plot localizes variables:

In[10]:=
x = 1;
Plot[{x, x^2}, {x, 0, Pi}]
Out[11]=

CheckboxLegended evaluates the plotting data before Plot, thereby undoing the localization:

In[12]:=
x = 1;
ResourceFunction["CheckboxLegended"][
 Plot[<|HoldForm[x] -> x, HoldForm[x^2] -> x^2 |>, {x, 0, Pi}]
 ]
Out[13]=

Use Block to circumvent this issue:

In[14]:=
x = 1;
Block[{x}, ResourceFunction["CheckboxLegended"][
  Plot[<|HoldForm[x] -> x, HoldForm[x^2] -> x^2 |>, {x, 0, Pi}]
  ]
 ]
Out[15]=

If you don’t specify the linking variable, CheckboxLegended will return a DynamicModule that cannot be combined with other graphics using Show:

In[16]:=
ResourceFunction["CheckboxLegended"][
 ListPlot[<|1 -> Range[5]|>]
 ]
Out[16]=

The output is a DynamicModule:

In[17]:=
Head[%]
Out[17]=

Therefore, Show cannot combine it with other graphics:

In[18]:=
Quiet@Show[%%, ListPlot[Abs@Sin[Range[5]], PlotStyle -> Red]]
Out[18]=

To combine graphics, use the optional second argument:

In[19]:=
DynamicModule[{
  visibleQ
  },
 Show[
  ResourceFunction["CheckboxLegended"][
   ListPlot[<|1 -> Range[5]|>],
   Dynamic[visibleQ]
   ],
  ListPlot[Abs@Sin[Range[5]], PlotStyle -> Red]
  ]
 ]
Out[19]=

When combining multiple wrappers, make sure that Style is on the outside. The checkboxes will not work if the Style wrapper is deeper inside:

In[20]:=
Clear[x]
ResourceFunction["CheckboxLegended"][
 Plot[<|HoldForm[x] -> Callout[x, "x", Above], HoldForm[x^2] -> Callout[Style[x^2, Red], ("x")^2, Right] |>, {x, 0, 1},
  PlotLabel -> "Checkbox broken"]
 ]
Out[21]=

It will work when it’s the outermost wrapper:

In[22]:=
ResourceFunction["CheckboxLegended"][
 Plot[<|HoldForm[x] -> Callout[x, "x", Above], HoldForm[x^2] -> Style[Callout[x^2, ("x")^2, Right], Red] |>, {x, 0, 1},
  PlotLabel -> "Checkboxes work"
  ]
 ]
Out[22]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information