Italics in title of Lattice graph
Asked Answered
A

1

1

I have read the posts on how to create italicized words in a graph title, but it does not seem to be working for me.

#create a list of species
sp <- c("Etelis coruscans","Etelis carbunculus","Pristipomoides sieboldii","Pristipomoides filamentosus","Pristipomoides zonatus","Epinephelus quernus","Aphareus rutilans")

#plot hisotgrams for each spp in 1cm bins
for (i in sp){
    BIN_WIDTH <- 1 #desired bin width
    print(histogram(~ Length..cm. | Method, #create and print the histogram and save to variable "graph"
    data = hist.data[hist.data$Scientific_name == i,], 
    nint = (max(hist.data$Length..cm.) - min(hist.data$Length..cm.)+1)/BIN_WIDTH,
    layout = c(1,2),
    main = paste("Length-Frequency of", italic(i), "by Gear"), sep = " ",
    xlab = "Length (cm)"))

    #save histogram to PNG file
    quartz.save(paste("*Length-Frequency of", i, "by method.png", sep = " "), type = "png")
    dev.off() #close the graphics diver
}

I get an error saying:

Error in print(histogram(~Length..cm. | Method, data = hist.data[hist.data$Scientific_name ==  : 
  error in evaluating the argument 'x' in selecting a method for function 'print': Error in paste(italic("Length-Frequency of", i, "by Gear")) : 
  could not find function "italic"

Can someone point out what I have done wrong?

Adigranth answered 20/5, 2013 at 22:2 Comment(0)
J
9

There argument you are passing to main needs a couple of changes.

  • To use R's plotmath specials (i.e. things like italic()), it should be an expression object rather than a character string. That means doing something like this:

    main = expression(paste("Length-Freq of", italic("E. coruscans"), "by Gear"))
    

    instead of this:

    main = paste("Length-Freq of", italic("E. coruscans"), "by Gear")
    
  • In addition, you are wanting to italicize i's value rather than its name, but if you just type italic(i), lattice will render i's name as a little italic "i" for each species. Use bquote() or substitute() to substitute in i's value instead, as demonstrated here:

    i <- "E. coruscans"
    xyplot(1:10~1:10,
        main = substitute(expr = expression(paste("Species name: ", italic(i))), 
                          env = list(i=i)))
    

enter image description here

Joon answered 20/5, 2013 at 22:26 Comment(1)
PERFECT! Here is the final working code: main = substitute(expr = expression(paste("Length-Frequency of ", italic(i), " by Gear")), env = list(i=i)),Adigranth

© 2022 - 2024 — McMap. All rights reserved.