1

I have a vector with different species names. For example:

sp_names<-c("sp1","sp2","sp3")

I have to make a map for each species and I need each species name written in italic in the title of the legend. Searching on the stackoverflow I found a suggestion to use susbtitute. I have tried like this:

legend(title=substitute(italic(sp_names), list(sp_names=sp_names[1])),
       "left",
       pch=21,col="black",pt.bg="darkgreen",
       legend = "Registro de ocorrĂȘncia", 
       cex = 0.7,
       bty = "n")

But I've got this error:

Error in legend(title = substitute(italic(title_leg), list(title_leg = title_leg[1])),  : 
  invalid 'title'

Could you help me? Thank you

Tiago
  • 143
  • 1
  • 2
  • 7

1 Answers1

2

According to the ?legend help page, the title= parameter must be

a character string or length-one expression giving a title to be placed at the top of the legend. Other objects will be coerced by as.graphicsAnnot.

and you are trying to pass in a "call" object. You can just coerce your call to an expression with as.expression()

legend(title=as.expression(substitute(italic(sp_names), list(sp_names=sp_names[1]))),
       "left",
       pch=21,col="black",pt.bg="darkgreen",
       legend = "Registro de ocorrĂȘncia", 
       cex = 0.7,
       bty = "n")
MrFlick
  • 195,160
  • 17
  • 277
  • 295