5
$\begingroup$

Consider the following graph:

Graph[
    DirectedEdge @@@ {
        {9, 10}, {8, 9}, {1, 9}, {21, 22}, {20, 21}, {1, 21}, {23, 24}, {10, 23}, {22, 23}
    },
    VertexLabels -> "Name", ImageSize -> Small
]

Graph

I would like to overlay a rectangle across the top row of nodes {8, 1, 20}. One of the possible forms of Inset is:

Inset[obj, pos, opos, size]

which seems like the right one to employ. So, I try:

Graph[
    DirectedEdge @@@ {
        {9, 10}, {8, 9}, {1, 9}, {21, 22}, {20, 21}, {1, 21}, {23, 24}, {10, 23}, {22, 23}
    },
    VertexLabels -> "Name", ImageSize -> Small,
    Epilog -> {
        Inset[
            Graphics[{RGBColor[0.25, 0.20, 0.25, 0.05], Rectangle[{0, 0}, {1, 1}]}],
            Scaled[{0, 1}], Scaled[{0, 1}], Scaled[{1, 0.2}]
        ]
    }
]

where the first Scaled[{0, 1}] for pos is (at least, intended to be) the top-left corner of the graph, and the second Scaled[{0, 1}] for opos is the top-left corner of the rectangle. The size Scaled[{1.0, 0.2}] is so that the overlay extends from left to right, while covering only a fifth of the height of the entire graph. But the expression does not render what I want:

Graph 2

I have tried tweaking all of the scaled parameters, without any success. Ideally, I would like the overlay to start a little above, and a little to the left of, the graph, so that the labels are also covered.

What is the correct way to achieve this?

$\endgroup$

2 Answers 2

5
$\begingroup$

Update: If you have to use Inset, you can add the option AspectRatio -> Full to Graphics[...] in the first argument of Inset (so that the the graphics primitives can be stretched based on size specified in the fourth argument of Inset):

Graph[DirectedEdge @@@ {{9, 10}, {8, 9}, {1, 9}, {21, 22}, {20,  21}, 
   {1, 21}, {23, 24}, {10, 23}, {22, 23}}, 
 VertexLabels -> "Name", ImageSize -> Small, 
 PlotRangePadding -> {Scaled /@ {.05, .15}, Scaled /@ {.05, .1}},
 Epilog -> {Inset[
    Graphics[{Opacity[.2, Red], Rectangle[]}, AspectRatio -> Full], 
    Scaled[{0, 1}], Scaled[{0, 1}], Scaled[{1, 0.2}]]}]

enter image description here

Graph[DirectedEdge @@@ {{9, 10}, {8, 9}, {1, 9}, {21, 22}, {20,  21}, 
   {1, 21}, {23, 24}, {10, 23}, {22, 23}}, 
 VertexLabels -> "Name", ImageSize -> Small, GraphLayout -> "SpringElectricalEmbedding",
 PlotRangePadding -> {Scaled /@ {.05, .1}, Scaled /@ {.05, .1}},
 Epilog -> {Inset[
    Graphics[{Opacity[.2, Red], Rectangle[]}, AspectRatio -> Full], 
    Scaled[{0, 1}], Scaled[{0, 1}], Scaled[{1, 0.2}]]}]

enter image description here

SeedRandom[1]
Graph[RandomGraph[{20, 30}], 
 VertexLabels -> "Name", ImageSize -> Medium, 
 PlotRangePadding -> Scaled[.05],
 Epilog -> {Inset[Graphics[{Opacity[.2, Red], Rectangle[]}, AspectRatio -> Full], 
     Scaled[{0, 1}], Scaled[{0, 1}], Scaled[{1, 0.2}]],
   Inset[Graphics[{Opacity[.2, Green], Rectangle[]}, AspectRatio -> Full], 
     Scaled[{0, .3}], Scaled[{0, 0}], Scaled[{1, 0.2}]]}]

enter image description here

Original answer: You can use Rectangle with Scaled coordinates:

Graph[DirectedEdge @@@ {{9, 10}, {8, 9}, {1, 9}, {21, 22}, {20, 21}, 
    {1, 21}, {23, 24}, {10, 23}, {22, 23}}, 
 VertexLabels -> "Name", ImageSize -> Small, 
 PlotRangePadding -> {Scaled /@ {.05, .15}, Scaled /@ {.05, .1}},
 Epilog -> {{RGBColor[0.25, 0.20, 0.25, 0.05], 
    Rectangle[Scaled[{0, .8}], Scaled[{1, 1}]], 
    RGBColor[0, 0.5, 0.1, 0.2], 
    Rectangle[Scaled[{0, .2}], Scaled[{1, .4}]]}}]

enter image description here

SeedRandom[1]
Graph[RandomGraph[{20, 30}], VertexLabels -> "Name", ImageSize -> Medium, 
 PlotRangePadding -> Scaled[.05],
 Epilog -> {{RGBColor[0.25, 0.20, 0.25, 0.05], 
    Rectangle[Scaled[{0, .8}], Scaled[{1, 1}]], 
    RGBColor[0, 0.5, 0.1, 0.2], 
    Rectangle[Scaled[{0, .3}], Scaled[{1, .5}]]}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you! AspectRatio and PlotRangePadding together did the trick for me. The latter was needed because in my actual graph the vertices are images of molecules. $\endgroup$ Commented Mar 28, 2022 at 18:03
5
$\begingroup$

You can use Show

Show[{Graph[
   DirectedEdge @@@ {{9, 10}, {8, 9}, {1, 9}, {21, 22}, {20, 21}, {1, 
      21}, {23, 24}, {10, 23}, {22, 23}}, VertexLabels -> "Name", 
   ImageSize -> Small, Frame -> True, FrameTicks -> All], 
  Graphics[{RGBColor[0.25, 0.20, 0.25, 0.05], 
    Rectangle[{-1.2, 4.1}, {1.5, 4.5}]}]}]

enter image description here

Show[{Graph[
   DirectedEdge @@@ {{9, 10}, {8, 9}, {1, 9}, {21, 22}, {20, 21}, {1, 
      21}, {23, 24}, {10, 23}, {22, 23}}, VertexLabels -> "Name", 
   ImageSize -> Small], 
  Graphics[{RGBColor[0.25, 0.20, 0.25, 0.05], 
    Rectangle[{-1.2, 3.1}, {1.5, 4.5}]}]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you very much for showing me the powerful general method of first displaying the frame, and then using the readings to overlay the graphics. This will be very useful in my subsequent work. $\endgroup$ Commented Mar 28, 2022 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.