How to label connected components in a disconnected graph?












1















I have some disconnected graph composed of three connected components. This graph is produced by the following commands in the igraph R:



library(igraph)
x1 <- c(1:7, 2, 8:14, 10, 15:21, 18)
x2 <- c(rep(0, 7), 1, rep(0, 7), 1, rep(0, 7), 1)
m <- cbind(x1, x2)
g <- graph.formula(1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 2-8,
9-10, 10-11, 11-12, 12-13, 13-14, 14-15, 11-16,
17-18, 18-19, 19-20, 20-21, 21-22, 22-23, 20-24)
plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
vertex.label = NA, edge.color = "black", vertex.color = "black")


The resulting disconnected graph is plotted below:
enter image description here



I want to label every disconnected components by a letter, for instance "A", "B" and "C". Alternatively, I want to make some subtitles for every connected components in the igraph R ?










share|improve this question





























    1















    I have some disconnected graph composed of three connected components. This graph is produced by the following commands in the igraph R:



    library(igraph)
    x1 <- c(1:7, 2, 8:14, 10, 15:21, 18)
    x2 <- c(rep(0, 7), 1, rep(0, 7), 1, rep(0, 7), 1)
    m <- cbind(x1, x2)
    g <- graph.formula(1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 2-8,
    9-10, 10-11, 11-12, 12-13, 13-14, 14-15, 11-16,
    17-18, 18-19, 19-20, 20-21, 21-22, 22-23, 20-24)
    plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
    vertex.label = NA, edge.color = "black", vertex.color = "black")


    The resulting disconnected graph is plotted below:
    enter image description here



    I want to label every disconnected components by a letter, for instance "A", "B" and "C". Alternatively, I want to make some subtitles for every connected components in the igraph R ?










    share|improve this question



























      1












      1








      1


      1






      I have some disconnected graph composed of three connected components. This graph is produced by the following commands in the igraph R:



      library(igraph)
      x1 <- c(1:7, 2, 8:14, 10, 15:21, 18)
      x2 <- c(rep(0, 7), 1, rep(0, 7), 1, rep(0, 7), 1)
      m <- cbind(x1, x2)
      g <- graph.formula(1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 2-8,
      9-10, 10-11, 11-12, 12-13, 13-14, 14-15, 11-16,
      17-18, 18-19, 19-20, 20-21, 21-22, 22-23, 20-24)
      plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
      vertex.label = NA, edge.color = "black", vertex.color = "black")


      The resulting disconnected graph is plotted below:
      enter image description here



      I want to label every disconnected components by a letter, for instance "A", "B" and "C". Alternatively, I want to make some subtitles for every connected components in the igraph R ?










      share|improve this question
















      I have some disconnected graph composed of three connected components. This graph is produced by the following commands in the igraph R:



      library(igraph)
      x1 <- c(1:7, 2, 8:14, 10, 15:21, 18)
      x2 <- c(rep(0, 7), 1, rep(0, 7), 1, rep(0, 7), 1)
      m <- cbind(x1, x2)
      g <- graph.formula(1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 2-8,
      9-10, 10-11, 11-12, 12-13, 13-14, 14-15, 11-16,
      17-18, 18-19, 19-20, 20-21, 21-22, 22-23, 20-24)
      plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
      vertex.label = NA, edge.color = "black", vertex.color = "black")


      The resulting disconnected graph is plotted below:
      enter image description here



      I want to label every disconnected components by a letter, for instance "A", "B" and "C". Alternatively, I want to make some subtitles for every connected components in the igraph R ?







      r igraph






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 0:30









      Henrik

      42.1k994110




      42.1k994110










      asked Nov 21 '18 at 22:51









      Piotr WilczekPiotr Wilczek

      60118




      60118
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.



          m <- cbind(m, id = components(g)$membership)
          xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
          ys <- tapply(m[ , "x2"], m[ , "id"], min)
          plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
          vertex.label = NA, edge.color = "black", vertex.color = "black")
          text(xs, ys - 0.6, LETTERS[1:3])


          enter image description here






          share|improve this answer





















          • 1





            Thanks for answer. This is a very good solution.

            – Piotr Wilczek
            Nov 22 '18 at 0:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421549%2fhow-to-label-connected-components-in-a-disconnected-graph%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.



          m <- cbind(m, id = components(g)$membership)
          xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
          ys <- tapply(m[ , "x2"], m[ , "id"], min)
          plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
          vertex.label = NA, edge.color = "black", vertex.color = "black")
          text(xs, ys - 0.6, LETTERS[1:3])


          enter image description here






          share|improve this answer





















          • 1





            Thanks for answer. This is a very good solution.

            – Piotr Wilczek
            Nov 22 '18 at 0:27
















          2














          Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.



          m <- cbind(m, id = components(g)$membership)
          xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
          ys <- tapply(m[ , "x2"], m[ , "id"], min)
          plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
          vertex.label = NA, edge.color = "black", vertex.color = "black")
          text(xs, ys - 0.6, LETTERS[1:3])


          enter image description here






          share|improve this answer





















          • 1





            Thanks for answer. This is a very good solution.

            – Piotr Wilczek
            Nov 22 '18 at 0:27














          2












          2








          2







          Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.



          m <- cbind(m, id = components(g)$membership)
          xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
          ys <- tapply(m[ , "x2"], m[ , "id"], min)
          plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
          vertex.label = NA, edge.color = "black", vertex.color = "black")
          text(xs, ys - 0.6, LETTERS[1:3])


          enter image description here






          share|improve this answer















          Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.



          m <- cbind(m, id = components(g)$membership)
          xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
          ys <- tapply(m[ , "x2"], m[ , "id"], min)
          plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
          vertex.label = NA, edge.color = "black", vertex.color = "black")
          text(xs, ys - 0.6, LETTERS[1:3])


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 16:05

























          answered Nov 21 '18 at 23:45









          HenrikHenrik

          42.1k994110




          42.1k994110








          • 1





            Thanks for answer. This is a very good solution.

            – Piotr Wilczek
            Nov 22 '18 at 0:27














          • 1





            Thanks for answer. This is a very good solution.

            – Piotr Wilczek
            Nov 22 '18 at 0:27








          1




          1





          Thanks for answer. This is a very good solution.

          – Piotr Wilczek
          Nov 22 '18 at 0:27





          Thanks for answer. This is a very good solution.

          – Piotr Wilczek
          Nov 22 '18 at 0:27




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421549%2fhow-to-label-connected-components-in-a-disconnected-graph%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini