R.D.
R.D.

Reputation: 9

Italics in the function 'plotweb', bipartite package R

I made a visualisation of a bipartite network using the function 'plotweb' from the bipartite package. Now I want to put the labels in italics (species names).

plotweb(datamatrix)

I can't find any useful command in the help tab for this function. Other suggestions?

Upvotes: 1

Views: 1547

Answers (1)

Valentin_Ștefan
Valentin_Ștefan

Reputation: 6416

Unfortunately, bipartite::plotweb doesn't seem to allow an argument like font = 3, which in graphics::par stands for italic font.

Here I suggest two solutions:

1 - Transform all text to italic with par(font = 3)

One way to get the desired italic font could be to force all the font of the graphical device to italic:

library(bipartite)

par(font = 3)
data(Safariland)
plotweb(Safariland)

When done with a graph and moving to another, you can run dev.off() to switch graphical parameters to their default original values. Or save the default par and rewrite when needed:

dpar <- par()

par(font = 3)
data(Safariland)
plotweb(Safariland)

par(dpar)

enter image description here

2 - Transform the plot to a gTree object and edit only what you need (grid package ideology)

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

However, this solution is far more verbose and convoluted. The advantage is that you can do almost any fine tuning you wish.

library(bipartite)
library(gridGraphics)

data(Safariland)

# Creates a gTree object from the "usual" plot generated by `plotweb`
my_gTree <- grid.grabExpr(grid.echo(function() plotweb(Safariland)))
# View the structure of the newly created gTree object
View(my_gTree)

# Example of editing the font of the text grobs in the range 28-36, 
# that is, the bottom labels (use regular expression)
my_gTree <- editGrob(grob = my_gTree,
                     gPath = "graphics-plot-1-text-2[8-9]|3[0-6]", 
                     gp = gpar(font = 3L),
                     grep = TRUE,
                     global = TRUE)
# Plot the edited graph
grid.newpage(); grid.draw(my_gTree)

If you wish, you can save it with ggplot2::ggsave()

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

enter image description here

If you want to edit a particular label, you can try:

my_gTree[["children"]][["graphics-plot-1-text-1"]][["gp"]][["font"]] <- 3L

Further more, if you want to match certain grob label by species name, then you could do something like below, where I only change to italics the species "Aristotelia chilensis" & "Schinus patagonicus". Unfortunately, it gets convoluted, but it proves the fine tuning that can be done with the grid package:

# Get all grobs referring to species labels
label_grobs <- getGrob(gTree = my_gTree, 
                       gPath = "graphics-plot-1-text-.*", 
                       grep = TRUE, 
                       global = TRUE)
# Get the species labels
sp <- rep(NA_character_, length(label_grobs))
for (i in 1:length(label_grobs)){
  sp[i] <- label_grobs[[i]][["label"]]
}
# Edit only those grobs of desired species
sp_idx <- which(sp %in% c("Aristotelia chilensis", "Schinus patagonicus"))
for (i in sp_idx){
  grob_char <- paste0("graphics-plot-1-text-", i) 
  my_gTree[["children"]][[grob_char]][["gp"]][["font"]] <- 3L
}
# display and save the edited gTree
grid.newpage(); grid.draw(my_gTree)
ggsave(filename = "plotweb-grid-edit-by-species.png", plot = my_gTree)

enter image description here

Upvotes: 0

Related Questions