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?
r bipartite
add a comment |
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?
r bipartite
add a comment |
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?
r bipartite
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
r bipartite
edited yesterday
Valentin
1,1731024
1,1731024
asked Aug 21 at 13:03
TaraC
62
62
add a comment |
add a comment |
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):
Here is the grid
version of it with the above edits:
grid.newpage(); grid.draw(my_gTree)
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 mpg
to 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.
add a comment |
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):
Here is the grid
version of it with the above edits:
grid.newpage(); grid.draw(my_gTree)
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 mpg
to 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.
add a comment |
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):
Here is the grid
version of it with the above edits:
grid.newpage(); grid.draw(my_gTree)
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 mpg
to 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.
add a comment |
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):
Here is the grid
version of it with the above edits:
grid.newpage(); grid.draw(my_gTree)
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 mpg
to 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.
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):
Here is the grid
version of it with the above edits:
grid.newpage(); grid.draw(my_gTree)
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 mpg
to 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.
edited yesterday
answered yesterday
Valentin
1,1731024
1,1731024
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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