How to add SwatchLegend to a graph?
You can use Legended
:
Legended[g1, legend]
Alternatively, you can use the option Epilog
:
RandomGraph[{100, 500}, VertexStyle -> color, VertexSize -> 1.5,
Epilog -> Inset[legend, Scaled @ {1.1, .5}], ImagePadding -> 40]
In general, you can use any type of legends inside the plotting routines:
Plot[
{Sin@x, Cos@x}, {x, -Pi, Pi},
PlotStyle -> {Red, Blue},
PlotLegends ->
Placed[SwatchLegend[{Red, Blue}, {"Sin", "Cos"}], Right]
]
However, the kglr's way allows making legends for any objects (like arbitrary graphics).