Change the position of labels in visweb() adjacency matrix plot from bipartite R package











up vote
1
down vote

favorite












I am conducting ecological network analysis, looking specifically at plant-pollinator mutualisms, using the bipartite package in R.



I have used the visweb() function to create an adjacency matrix using the standard code provided in the bipartite package guide. I want to change the position of the lower level species label as it's currently skewed to the left, but I can't see anything in the package guide which tells you how to do it It currently looks like this.



Could anyone help?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I am conducting ecological network analysis, looking specifically at plant-pollinator mutualisms, using the bipartite package in R.



    I have used the visweb() function to create an adjacency matrix using the standard code provided in the bipartite package guide. I want to change the position of the lower level species label as it's currently skewed to the left, but I can't see anything in the package guide which tells you how to do it It currently looks like this.



    Could anyone help?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am conducting ecological network analysis, looking specifically at plant-pollinator mutualisms, using the bipartite package in R.



      I have used the visweb() function to create an adjacency matrix using the standard code provided in the bipartite package guide. I want to change the position of the lower level species label as it's currently skewed to the left, but I can't see anything in the package guide which tells you how to do it It currently looks like this.



      Could anyone help?










      share|improve this question















      I am conducting ecological network analysis, looking specifically at plant-pollinator mutualisms, using the bipartite package in R.



      I have used the visweb() function to create an adjacency matrix using the standard code provided in the bipartite package guide. I want to change the position of the lower level species label as it's currently skewed to the left, but I can't see anything in the package guide which tells you how to do it It currently looks like this.



      Could anyone help?







      r bipartite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Valentin

      1,1731024




      1,1731024










      asked Aug 21 at 13:03









      TaraC

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.



          I suggest two solutions, both a bit convoluted:



          1 - Transform the plot to gTree object (grid package ideology)



          You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").



          I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:



          library(bipartite)
          library(gridGraphics)

          data(Safariland) # load data
          # Creates a gTree object from the "usual" plot generated by `visweb`
          my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))


          View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:



          View(my_gTree)
          # shift the left axis labels to the right
          my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
          # shift the bottom axis labels upwards
          my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")


          This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom):
          enter image description here



          Here is the grid version of it with the above edits:



          grid.newpage(); grid.draw(my_gTree)


          enter image description here



          Even more, if you wish, you can save it with ggplot2::ggsave() function:



          library(ggplot2)
          ggsave(filename = "visweb-grid-version.png", plot = my_gTree)


          2 - Adapt/Edit visweb to your needs



          If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:



           axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
          mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


          What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:



           axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
          mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


          and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).



          As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.






          share|improve this answer























            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',
            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%2f51949334%2fchange-the-position-of-labels-in-visweb-adjacency-matrix-plot-from-bipartite-r%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








            up vote
            0
            down vote













            Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.



            I suggest two solutions, both a bit convoluted:



            1 - Transform the plot to gTree object (grid package ideology)



            You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").



            I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:



            library(bipartite)
            library(gridGraphics)

            data(Safariland) # load data
            # Creates a gTree object from the "usual" plot generated by `visweb`
            my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))


            View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:



            View(my_gTree)
            # shift the left axis labels to the right
            my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
            # shift the bottom axis labels upwards
            my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")


            This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom):
            enter image description here



            Here is the grid version of it with the above edits:



            grid.newpage(); grid.draw(my_gTree)


            enter image description here



            Even more, if you wish, you can save it with ggplot2::ggsave() function:



            library(ggplot2)
            ggsave(filename = "visweb-grid-version.png", plot = my_gTree)


            2 - Adapt/Edit visweb to your needs



            If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:



             axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
            mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


            What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:



             axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
            mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


            and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).



            As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.






            share|improve this answer



























              up vote
              0
              down vote













              Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.



              I suggest two solutions, both a bit convoluted:



              1 - Transform the plot to gTree object (grid package ideology)



              You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").



              I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:



              library(bipartite)
              library(gridGraphics)

              data(Safariland) # load data
              # Creates a gTree object from the "usual" plot generated by `visweb`
              my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))


              View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:



              View(my_gTree)
              # shift the left axis labels to the right
              my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
              # shift the bottom axis labels upwards
              my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")


              This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom):
              enter image description here



              Here is the grid version of it with the above edits:



              grid.newpage(); grid.draw(my_gTree)


              enter image description here



              Even more, if you wish, you can save it with ggplot2::ggsave() function:



              library(ggplot2)
              ggsave(filename = "visweb-grid-version.png", plot = my_gTree)


              2 - Adapt/Edit visweb to your needs



              If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:



               axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
              mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


              What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:



               axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
              mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


              and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).



              As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.



                I suggest two solutions, both a bit convoluted:



                1 - Transform the plot to gTree object (grid package ideology)



                You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").



                I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:



                library(bipartite)
                library(gridGraphics)

                data(Safariland) # load data
                # Creates a gTree object from the "usual" plot generated by `visweb`
                my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))


                View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:



                View(my_gTree)
                # shift the left axis labels to the right
                my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
                # shift the bottom axis labels upwards
                my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")


                This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom):
                enter image description here



                Here is the grid version of it with the above edits:



                grid.newpage(); grid.draw(my_gTree)


                enter image description here



                Even more, if you wish, you can save it with ggplot2::ggsave() function:



                library(ggplot2)
                ggsave(filename = "visweb-grid-version.png", plot = my_gTree)


                2 - Adapt/Edit visweb to your needs



                If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:



                 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
                mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


                What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:



                 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
                mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


                and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).



                As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.






                share|improve this answer














                Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.



                I suggest two solutions, both a bit convoluted:



                1 - Transform the plot to gTree object (grid package ideology)



                You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").



                I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:



                library(bipartite)
                library(gridGraphics)

                data(Safariland) # load data
                # Creates a gTree object from the "usual" plot generated by `visweb`
                my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))


                View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:



                View(my_gTree)
                # shift the left axis labels to the right
                my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
                # shift the bottom axis labels upwards
                my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")


                This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom):
                enter image description here



                Here is the grid version of it with the above edits:



                grid.newpage(); grid.draw(my_gTree)


                enter image description here



                Even more, if you wish, you can save it with ggplot2::ggsave() function:



                library(ggplot2)
                ggsave(filename = "visweb-grid-version.png", plot = my_gTree)


                2 - Adapt/Edit visweb to your needs



                If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:



                 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
                mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


                What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:



                 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
                mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)


                and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).



                As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                Valentin

                1,1731024




                1,1731024






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51949334%2fchange-the-position-of-labels-in-visweb-adjacency-matrix-plot-from-bipartite-r%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







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu